Part Number Hot Search : 
AM79C UM3865P INTEL C1892CT 31M10 NS0013A 2SB1400 Z3018
Product Description
Full Text Search
 

To Download PIC18F14K22 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  ? 2009-2016 microchip technology inc. ds40001365f-page 1 pic18(l)f1xk22 high-performance risc cpu c compiler optimized architecture: - optional extended instruction set designed to optimize re-entrant code 256 bytes data eeprom up to 16 kbytes linear program memory addressing up to 512 bytes linear data memory addressing up to 16 mips operation 16-bit wide instructions, 8-bit wide data path priority levels for interrupts 31-level, software accessible hardware stack 8 x 8 single-cycle hardware multiplier flexible oscillator structure precision 16 mhz internal oscillator block: - factory calibrated to 1% - software selectable frequencies range of 31 khz to 16 mhz - 64 mhz performance available using pll C no external components required four crystal modes up to 64 mhz two external clock modes up to 64 mhz 4x phase lock loop (pll) secondary oscillator using timer1 @ 32 khz fail-safe clock monitor - allows for safe shutdown if peripheral clock stops two-speed oscillator start-up special microcontroller features 2.3v - 5.5v operation C pic18f1xk22 1.8v-3.6v operation C pic18lf1xk22 self-reprogrammable under software control power-on reset (por), power-up timer (pwrt) and oscillator start-up timer (ost) programmable brown-out reset (bor) extended watchdog timer (wdt): - programmable period from 4 ms to 131s programmable code protection in-circuit serial programming? (icsp?) via two pins in-circuit debug via two pins extreme low-power management pic18lf1xk22 with xlp technology sleep mode: 34 na watchdog timer: 460 na timer1 oscillator: 650 na @ 32 khz analog features analog-to-digital converter (adc) module - 10-bit resolution, 12 channels - auto-acquisition capability - conversion available during sleep analog comparator module: - two rail-to-rail analog comparators - independent input multiplexing - inputs and outputs externally accessible voltage reference module: - fixed voltage reference (fvr) with 1.024v, 2.048v and 4.096v output levels - 5-bit rail-to-rail resistive digital-to-analog converter (dac) with positive and negative reference selection peripheral highlights 17 i/o pins and 1 input-only pin: - high current sink/source 25 ma/25 ma - programmable weak pull-ups - programmable interrupt-on- change - three external interrupt pins four timer modules: - three 16-bit timers/counters with prescaler - one 8-bit timer/counter with 8-bit period register, prescaler and postscaler - dedicated, low-power timer1 oscillator enhanced capture/compare/pwm (eccp) module: - one, two or four pwm outputs - selectable polarity - programmable dead time - auto-shutdown and auto-restart - pwm output steering control master synchronous serial port (mssp) module - 3-wire spi (supports all four spi modes) -i 2 c master and slave modes (slave mode address masking) enhanced universal synchronous asynchronous receiver transmitter module (eusart) - supports rs-232, rs-485 and lin 2.0 - auto-baud detect - auto wake-up on break sr latch (555 timer) module with: - configurable inputs and outputs - supports mtouch ? capacitive sensing applications 20-pin flash microcontrollers with xlp technology downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 2 ? 2009-2016 microchip technology inc. pic18(l)f1xk22 family types pin diagrams figure 1: 20-pin pdip, ssop, soic figure 2: 20-pin qfn (4x4) device data sheet index program memory data memory pins i/o (1) 10-bit a/d channels comparators timers 8-bit/16-bit eccp mssp eusart sr latch bytes words sram (bytes) data eeprom (bytes) pic18(l)f13k22 (1) 8k 4k 256 256 20 18 12-ch 2 1 / 3 1 1 1 yes pic18(l)f14k22 (1) 16k 8k 512 256 20 18 12-ch 2 1 / 3 1 1 1 yes note 1: one pin is input-only. data sheet index : (unshaded devices are described in this document) 1. ds40001365 pic18(l)f1xk22 20-pin flash microcontrollers with xlp technology note: for other small form-factor package availability and marking information, please visit http://www.microchip.com/packaging or contact your local sales office. 10 23 4 5 6 18 79 11 12 13 14 15 16 19 2018 17 v dd ra5 ra4 ra3/mclr /v pp rc5rc4 rc3/pgm rc6rc7 rb7 v ss ra0/pgd ra1/pgc ra2 rc0 rc1 rc2 rb4 rb5 rb6 pic18(l)f13k22 pic18(l)f14k22 note: see table 1 for location of all peripheral functions. 89 23 1 14 15 16 10 11 6 12 13 17 18 19 20 7 5 4 pic18(l)f13k22 pic18(l)f14k22 ra3/mclr /v pp rc5 rc4 rc3/pgm rc6 rc7 rb7 rb4 rb5 rb6 rc1 rc0 ra2 ra1/pgc ra0/pgd v ss v dd ra4 ra5 rc2 note: see table 1 for location of all peripheral functions. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 3 pic18(l)f1xk22 table 1: 20-pin allocation table (pic18(l)f1xk22) i/o 20-pin pdip/ssop/soic 20-pin qfn analog comparator reference eccp eusart mssp sr latch timers interrupts pull-up basic ra0 19 16 an0 c1in+ v ref -/ cv ref (dac1out) ioc/int0 y pgd ra1 18 15 an1 c12in0- v ref + ioc/int1 y pgc ra2 17 14 an2 c1out srq t0cki ioc/int2 y ra3 4 1 ioc y mclr /v pp ra4 3 20 an3 ioc y osc2/clkout ra5 2 19 t13cki ioc y osc1/clkin rb4 13 10 an10 sdi/sda ioc y rb5 12 9 an11 rx/dt ioc y rb6 11 8 scl/sck ioc y rb7 10 7 tx/ck ioc y rc0 16 13 an4 c2in+ rc1 15 12 an5 c12in1- rc2 14 11 an6 c12in2- p1d rc3 7 4 an7 c12in3- p1c pgm rc4 6 3 c2out p1b srnq rc5 5 2 ccp1/p1a rc6 8 5 an8 ss rc7 9 6 an9 sdo 1 18 v dd 2 01 7 v ss downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 4 ? 2009-2016 microchip technology inc. table of contents 1.0 device overview ............................................................................ ................................. ............................................................. 6 2.0 oscillator module................................................................................ ........................... ............................................................. 12 3.0 memory organization .......................................... ................................................... ............ ........................................................ 24 4.0 flash program memory.................................................................. ...................................... ...................................................... 45 5.0 data eeprom memory ..................................................................... ..................................... ................................................... 54 6.0 8 x 8 hardware multiplier............................................... .................................................... ......................................................... 58 7.0 interrupts .................................................................................... ................................................................................................ 60 8.0 i/o ports ......................................................................................... .......................... .................................................................. 73 9.0 timer0 module .............................................. ................................................... .............. ............................................................ 91 10.0 timer1 module .............................................. ................................................... ............. ............................................................. 94 11.0 timer2 module .............................................. ................................................... ............. ........................................................... 100 12.0 timer3 module .............................................. ................................................... ............. ........................................................... 102 13.0 enhanced capture/compare/pwm (eccp) module.................................... ............................................. ............................... 106 14.0 master synchronous serial port (mssp) module ................................. .............................................. ..................................... 127 15.0 enhanced universal synchronous asynchronous receiver transmitter (eusart) ....................................... ........... ............. 170 16.0 analog-to-digital converter (adc) module ..................................... .............................................. ........................................... 197 17.0 comparator module......................................... ................................................... .............. ........................................................ 210 18.0 power-managed modes ............................................ ................................................... ......... ................................................... 222 19.0 sr latch.................................................................. ................................................. ................................................................ 228 20.0 fixed voltage reference (fvr).................................................. ............................................ .................................................. 231 21.0 digital-to-analog converter (dac) module ....................................... ............................................ ........................................... 233 22.0 reset .......................................................................................... ............................ .................................................................. 237 23.0 special features of the cpu ...................................................... .......................................... .................................................... 249 24.0 instruction set summary ....................................................... ............................................. ...................................................... 265 25.0 development support............................................ ................................................... ......... ....................................................... 315 26.0 electrical specifications........................................... ....................................................... .......................................................... 319 27.0 dc and ac characteristics graphs and charts ................................... ............................................. ....................................... 356 28.0 packaging information............................................. ......................................................... ........................................................ 372 appendix a: revision history.......................................... ................................................... ...... .......................................................... 382 appendix b: device differences........................................... ................................................... ... ........................................................ 383 the microchip website ............................................................... ........................................... ............................................................ 384 customer change notification service ....................................... ................................................... . ................................................... 384 customer support ............................................... ................................................... ............. ............................................................... 384 product identification system.................................................. ................................................................................................. .......... 385 worldwide sales and service ...................................................................... .............................. ........................................................ 387 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 5 pic18(l)f1xk22 to our valued customers it is our intention to provide our valued customers with the best documentation possible to ensure successful use of your micro chip products. to this end, we will continue to improve our publications to better suit your needs. our publications will be refined and enhanced as new volumes and updates are introduced. if you have any questions or comments regar ding this publication, please contact the marketing communications department via e-mail at docerrors@microchip.com . we welcome your feedback. most current data sheet to obtain the most up-to-date version of this data sheet, please register at our worldwide website at: http://www.microchip.com you can determine the version of a data sheet by examining its literature number found on the bottom outside corner of any page . the last character of the literature number is the versi on number, (e.g., ds30000000a is ve rsion a of document ds30000000). errata an errata sheet, describing minor operational differences fr om the data sheet and recommended workarounds, may exist for curren t devices. as device/documentation issues become known to us, we will publish an errata sheet. the errata will specify the revisi on of silicon and revision of document to which it applies. to determine if an errata sheet exists for a particular device, please check with one of the following: microchips worldwide website; http://www.microchip.com your local microchip sales office (see last page) when contacting a sales office, please specify which device, re vision of silicon and data sheet (include literature number) you are using. customer notification system register on our website at www.microchip.com to receive the most current information on all of our products. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 6 ? 2009-2016 microchip technology inc. 1.0 device overview this family offers the advantages of all pic18 microcontrollers C namely, high computational performance with the addition of high-endurance, flash program memory. on top of these features, the pic18(l)f1xk22 family introduces design enhancements that make these microcontrollers a logical choice for many high-performance, power sensitive applications. 1.1 new core features 1.1.1 xlp technology all of the devices in the pic18(l)f1xk22 family incorporate a range of features that can significantly reduce power consumption during operation. key items include: multiple idle modes: the controller can also run with its cpu core disabled but the peripherals still active. in these states, power consumption can be reduced even further, to as little as 4% of normal operation requirements. on-the-fly mode switching: the power-managed modes are invoked by user code during operation, allowing the user to incorporate power-saving ideas into their applications software design. low consumption in key modules: the power requirements for both timer1 and the watchdog timer are minimized. see section 26.0 ?electrical specifications? for values. 1.1.2 multiple oscillator options and features all of the devices in the pic18(l)f1xk22 family offer ten different oscillator options, allowing users a wide range of choices in developing application hardware. these include: four crystal modes, using crystals or ceramic resonators external clock modes, offering the option of using two pins (oscillator input and a divide-by-4 clock output) or one pin (oscillator input, with the second pin reassigned as general i/o) external rc oscillator modes with the same pin options as the external clock modes an internal oscillator block which contains a 16 mhz hfintosc oscillator and a 31 khz lfintosc oscillator which together provide eight user selectable clock frequencies, from 31 khz to 16 mhz. this option frees the two oscillator pins for use as additional general purpose i/o. a phase lock loop (pll) frequency multiplier, available to both the high-speed crystal and internal oscillator modes, which allows clock speeds of up to 64 mhz. used with the internal oscillator, the pll gives users a complete selection of clock speeds, from 31 khz to 64 mhz C all without using an external crystal or clock circuit. besides its availability as a clock source, the internal oscillator block provides a stable reference source that gives the family additional features for robust operation: fail-safe clock monitor: this option constantly monitors the main clock source against a reference signal provided by the lfintosc. if a clock failure occurs, the controller is switched to the internal oscillator block, allowing for continued operation or a safe application shutdown. two-speed start-up: this option allows the internal oscillator to serve as the clock source from power-on reset, or wake-up from sleep mode, until the primary clock source is available. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 7 pic18(l)f1xk22 1.2 other special features memory endurance: the flash cells for both program memory and data eeprom are rated to last for many thousands of erase/write cycles C up to 10k for program memory and 100k for eeprom. data retention without refresh is conservatively estimated to be greater than 40 years. self-programmability: these devices can write to their own program memory spaces under internal software control. using a bootloader routine located in the code protected boot block, it is possible to create an application that can update itself in the field. extended instruction set: the pic18(l)f1xk22 family introduces an optional extension to the pic18 instruction set, which adds eight new instructions and an indexed addressing mode. this extension has been specifically designed to optimize re-entrant application code originally developed in high-level languages, such as c. enhanced ccp module: in pwm mode, this module provides one, two or four modulated outputs for controlling half-bridge and full-bridge drivers. other features include: - auto-shutdown, for disabling pwm outputs on interrupt or other select conditions - auto-restart, to reactivate outputs once the condition has cleared - output steering to selectively enable one or more of four outputs to provide the pwm signal. enhanced addressable usart: this serial communication module is capable of standard rs-232 operation and provides support for the lin bus protocol. other enhancements include automatic baud rate detection and a 16-bit baud rate generator for improved resolution. 10-bit a/d converter: this module incorporates programmable acquisition time, allowing for a channel to be selected and a conversion to be initiated without waiting for a sampling period and thus, reduce code overhead. extended watchdog timer (wdt): this enhanced version incorporates a 16-bit postscaler, allowing an extended time-out range that is stable across operating voltage and temperature. see section 26.0 ?electrical specifications? for time-out periods. 1.3 details on individual family members devices in the pic18(l)f1xk22 family are available in 20-pin packages. block diagrams for the two groups are shown in figure 1-1 . the devices are differentiated from each other in the following ways: 1. flash program memory: 8 kbytes for pic18(l)f13k22 16 kbytes for pic18(l)f14k22 all other features for devices in this family are identical. these are summarized in ta b l e 1 - 1 . the pinouts for all devices are listed in tab l e 1 and i/o description are in table 1-2 . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 8 ? 2009-2016 microchip technology inc. table 1-1: device features for the pi c18(l)f1xk22 (20-pin devices) features pic18f13k22 pic18lf13k22 PIC18F14K22 pic18lf14k22 voltage range (1.8 - 5.5v) 2.3-5.5v 1.8v-3.6v 2.3-5.5v 1.8v-3.6v program memory (bytes) 8k 16k program memory (instructions) 4096 8192 data memory (bytes) 256 512 operating frequency dc C 64 mhz interrupt sources 30 i/o ports ports a, b, c timers 4 enhanced capture/ compare/pwm modules 1 serial communications mssp, enhanced usart 10-bit analog-to-digital module 12 input channels resets (and delays) por, bor, reset instruction, stack full, stack underflow, mclr , wdt (pwrt, ost) instruction set 75 instructions, 83 with extended instruction set enabled packages 20-pin pdip, ssop, soic qfn (4x4x0.9mm) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 9 pic18(l)f1xk22 figure 1-1: pic18(l)f1xk22 block diagram instruction decode and control porta portb portc ra1 ra0 data latch data memory address latch data address<12> 12 access bsr fsr0 fsr1 fsr2 inc/dec logic address 4 12 4 pch pcl pclath 8 31-level stack program counter prodl prodh 8 x 8 multiply 8 bitop 8 8 alu<8> 20 8 8 table pointer<21> inc/dec logic 21 8 data bus<8> table latch 8 ir 12 3 rom latch pclatu pcu note 1: ra3 is only available when mclr functionality is disabled. 2: osc1/clkin and osc2/clkout are only available in select oscillator modes and when these pins are not being used as digital i/o. refer to section 2.0 ?oscillator module? for additional information. eusart comparator mssp 10-bit adc timer2 timer1 timer3 timer0 eccp1 bor data eeprom w instruction bus <16> stkptr bank 8 state machine control signals decode 8 8 power-up timer oscillator start-up timer power-on reset watchdog timer osc1 (2) osc2 (2) v dd , internal oscillator fail-safe clock monitor precision reference band gap v ss mclr (1) block lfintosc oscillator 16 mhz oscillator single-supply programming fvr fvr fvr cv ref /dac1 address latch program memory data latch ra3 ra4 ra5 rb4rb5 rb6 rb7 rc0 rc1 rc2 rc3 rc4 rc5 rc6 rc7 (512/768 bytes) ra1 dac cv ref /dac1 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 10 ? 2009-2016 microchip technology inc. table 1-2: pic18(l)f1xk22 pin summary pin name pin number pin type buffer type description pdip/ssop/ soic qfn ra0/an0/cv ref /v ref -/c1in+/int0/pgd ra0 an0 cv ref /dac1out v ref - c1in+ int0 pgd 19 16 i/o i o ii i i/o ttl analog analog analog analog stst digital i/o adc channel 0 dac reference voltage output adc and dac reference voltage (low) input comparator c1 noninverting input external interrupt 0 icsp? programming data pin ra1/an1/c12in0-/v ref +/int1/pgc ra1 an1 c12in0- v ref + int1 pgc 18 15 i/o i 1 ii i/o ttl analog analog analog stst digital i/o adc channel 1 comparator c1 and c2 inverting input adc and dac reference voltage (high) input external interrupt 1 icsp programming clock pin ra2/an2/c1out/t0cki/int2/srq ra2 an2 c1out t0cki int2 srq 17 14 i/o i ii o st analog cmos stst cmos digital i/o adc channel 2 comparator c1 output timer0 external clock input external interrupt 2 sr latch output ra3/mclr /v pp ra3 mclr v pp 41 ii p stst digital input active-low master clear with internal pull-up high voltage programming input ra4/an3/osc2/clkout ra4 an3 osc2 clkout 320 i/o i oo ttl analog xtal cmos digital i/o adc channel 3 oscillator crystal output. connect to crystal or resonator in crystal oscillator mode in rc mode, osc2 pin outputs clkout which has 1/4 the frequency of osc1 and denotes the instruction cycle rate ra5/osc1/clkin/t13cki ra5 osc1 clkin t13cki 219 i/o ii i ttl xtal cmos st digital i/o oscillator crystal input or external clock input st buffer when configured in rc mode; analog other wise external clock source input. always associated with the pin function osc1 (see related osc1/clkin, osc2, clkout pins timer0 and timer3 external clock input rb4/an10/sdi/sda rb4 an10 sdi sda 13 10 i/o ii i/o ttl analog stst digital i/o adc channel 10 spi data in i 2 c data i/o legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input i = input o = output p = power xtal= crystal oscillator downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 11 pic18(l)f1xk22 rb5/an11/rx/dt rb5 an11 rx dt 12 9 i/o ii i/o tll analog stst digital i/o adc channel 11 eusart asynchronous receive eusart synchronous data (see related rx/tx) rb6/sck/scl rb6 sck scl 11 8 i/oi/o i/o tll stst digital i/o synchronous serial clock input/output for spi mode synchronous serial clock input/output for i 2 c mode rb7/tx/ck rb7 tx ck 10 7 i/o o i/o tll cmos st digital i/o eusart asynchronous transmit eusart synchronous clock (see related rx/dt) rc0/an4/c2in+ rc0 an4 c2in+ 16 13 i/o ii st analog analog digital i/o adc channel 4 comparator c2 noninverting input rc1/an5/c12in- rc1 an5 c12in- 15 12 i/o ii st analog analog digital i/o adc channel 5 comparator c1 and c2 inverting input rc2/an6/c12in2-/p1d rc2 an6 c12in2- p1d 14 11 i/o ii o st analog analog cmos digital i/o adc channel 6 comparator c1 and c2 inverting input enhanced ccp1 pwm output rc3/an7/c12in3-/p1c/pgm rc3 an7 c12in3- p1c pgm 74 i/o ii o i/o st analog analog cmos st digital i/o adc channel 7 comparator c1 and c2 inverting input enhanced ccp1 pwm output low-voltage icsp programming enable pin rc4/c2out/p1b/srnq rc4 c2out p1b srnq 63 i/o oo o st cmos cmos cmos digital i/o comparator c2 output enhanced ccp1 pwm output sr latch inverted output rc5/ccp1/p1a rc5 ccp1 p1a 52 i/oi/o o stst cmos digital i/o capture 1 input/compare 1 output/pwm 1 output enhanced ccp1 pwm output rc6/an8/ss rc6 an8 ss 85 i/o ii st analog ttl digital i/o adc channel 8 spi slave select input rc7/an9/sdo rc7 an9 sdo 96 i/o i o st analog cmos digital i/o adc channel 9 spi data out v ss 20 17 p ground reference for logic and i/o pins v dd 1 18 p positive supply for logic and i/o pins table 1-2: pic18(l)f1xk22 pin summary (continued) pin name pin number pin type buffer type description pdip/ssop/ soic qfn legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input i = input o = output p = power xtal= crystal oscillator downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 12 ? 2009-2016 microchip technology inc. 2.0 oscillator module 2.1 overview the oscillator module has a variety of clock sources and features that allow it to be used in a wide range of applications, maximizing performance and minimizing power consumption. figure 2-1 illustrates a block diagram of the oscillator module. key features of the oscillator module include: system clocks system clock selection - primary external oscillator - secondary external oscillator - internal oscillator oscillator start-up timer system clock selection clock switching 4x phase lock loop frequency multiplier cpu clock divider two-speed start-up mode fail-safe clock monitoring 2.2 system clocks the pic18(l)f1xk22 can be operated in 13 different oscillator modes. the user can program these using the available configuration bits. in addition, clock support functions such as fail-safe and two start-up can also be configured. the available primary oscillator options include: external clock, low power (ecl) external clock, medium power (ecm) external clock, high power (ech) external clock, low power, clkout function on ra4/osc2 (ecclkoutl) external clock, medium power, clkout function on ra4/osc2 (ecclkoutm) external clock, high power, clkout function on ra4/osc2 (ecclkouth) external crystal (xt) high-speed crystal (hs) low-power crystal (lp) external resistor/capacitor (extrc) external rc, clkout function on ra4/osc2 31.25 khz C 16 mhz internal oscillator (intosc) 31.25 khz C 16 mhz internal oscillator, clkout function on ra4/osc2 additionally, the 4x pll may be enabled in hardware or software (under certain conditions) for increased oscillator speed. 2.3 system clock selection the scs bits of the osccon register select between the following clock sources: primary external oscillator secondary external oscillator internal oscillator table 2-1: system clock selection the default state of the scs bits sets the system clock to be the oscillator defined by the fosc bits of the config1h configuration register. the system clock will always be defined by the fosc bits until the scs bits are modified in software. when the internal oscillator is selected as the system clock, the ircf bits of the osccon register and the intsrc bit of the osctune register will select either the lfintosc or the hfintosc. the lfintosc is selected when the ircf<2:0> = 000 and the intsrc bit is clear. all other combinations of the ircf bits and the intsrc bit will select the hfintosc as the system clock. 2.4 primary external oscillator the primary external oscillators mode of operation is selected by setting the fosc<3:0> bits of the config1h configuration register. the oscillator can be set to the following modes: lp: low-power crystal xt: crystal/ceramic resonator hs: high-speed crystal resonator rc: external rc oscillator ec: external clock additionally, the primary external oscillator may be shut down under firmware control to save power. note: the frequency of the system clock will be referred to as f osc throughout this document. configuration selection scs <1:0> system clock 1x internal oscillator 01 secondary external oscillator 00 (default after reset) oscillator defined by fosc<3:0> downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 13 pic18(l)f1xk22 figure 2-1: pic ? mcu clock source block diagram 4 x pll fosc<3:0> osc2 osc1/t13cki sleep cpu peripherals idlen postscaler mux mux 16 mhz 8 mhz 4 mhz 2 mhz 1 mhz 250 khz 500 khz ircf<2:0> 0x 110101 100 011 010 001 000 31 khz 31 khz lfintosc internal oscillator block clock control scs<1:0> hfintosc 16 mhz 0 1 intsrc primary pic18(l)f1xk22 sleep sleep system pclken pri_sd 0 1 fosc<3:0> pll_en pllen oscillator, watchdog timer fail-safe clock two-speed start-up clock timer1/timer3 external secondary oscillator and lp, xt, hs, rc, ec, secondary osc. t1oscen internal osc. 1x note: if using a low-frequency external oscillator and want to multiple it by 4 via pll, the ideal input frequency is from 4 mhz to 16 mhz. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 14 ? 2009-2016 microchip technology inc. 2.4.1 primary external oscillator shutdown the primary external oscillator can be enabled or disabled via software. to enable software control of the primary external oscillator, the pclken bit of the config1h configuration register must be set. with the pclken bit set, the primary external oscillator is controlled by the pri_sd bit of the osccon2 register. the primary external oscillator will be enabled when the pri_sd bit is set, and disabled when the pri_sd bit is clear. 2.4.2 lp, xt and hs oscillator modes the lp, xt and hs modes support the use of quartz crystal resonators or ceramic resonators connected to osc1 and osc2 ( figure 2-2 ). the mode selects a low, medium or high gain setting of the internal inverter- amplifier to support various resonator types and speed. lp oscillator mode selects the lowest gain setting of the internal inverter-amplifier. lp mode current consumption is the least of the three modes. this mode is best suited to drive resonators with a low drive level specification, for example, tuning fork type crystals. xt oscillator mode selects the intermediate gain setting of the internal inverter-amplifier. xt mode current consumption is the medium of the three modes. this mode is best suited to drive resonators with a medium drive level specification. hs oscillator mode selects the highest gain setting of the internal inverter-amplifier. hs mode current consumption is the highest of the three modes. this mode is best suited for resonators that require a high drive setting. figure 2-2 and figure 2-3 show typical circuits for quartz crystal and ceramic resonators, respectively. figure 2-2: quartz crystal operation (lp, xt or hs mode) note: the primary external oscillator cannot be shut down when it is selected as the system clock. to shut down the oscillator, the system clock source must be either the secondary oscillator or the internal oscillator. note 1: quartz crystal characteristics vary according to type, package and manufacturer. the user should consult the manufacturer data sheets for specifications and recommended application. 2: always verify oscillator performance over the v dd and temperature range that is expected for the application. 3: for oscillator design assistance, reference the following microchip applications notes: an826, crystal oscillator basics and crystal selection for rfpic ? and picmicro ? devices (ds00826) an849, basic picmicro ? oscillator design (ds00849) an943, practical picmicro ? oscillator analysis and design (ds00943) an949, making your oscillator work (ds00949) note 1: a series resistor (r s ) may be required for quartz crystals with low drive level. 2: the value of r f varies with the oscillator mode selected (typically between 2 m ? to 10 m ?? . c1 c2 quartz r s (1) osc1/clkin r f (2) sleep to internal logic pic ? mcu crystal osc2/clkout downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 15 pic18(l)f1xk22 figure 2-3: ceramic resonator operation (xt or hs mode) 2.4.3 external rc the external resistor-capacitor (rc) mode supports the use of an external rc circuit. this allows the designer maximum flexibility in frequency choice while keeping costs to a minimum when clock accuracy is not required. in rc mode, the rc circuit connects to osc1, allowing osc2 to be configured as an i/o or as clkout. the clkout function is selected by the fosc bits of the config1h configuration register. when osc2 is configured as clkout, the frequency at the pin is the frequency of the rc oscillator divided by 4. figure 2-4 shows the external rc mode connections. figure 2-4: external rc modes the rc oscillator frequency is a function of the supply voltage, the resistor r ext , the capacitor c ext and the operating temperature. other factors affecting the oscillator frequency are: input threshold voltage variation component tolerances variation in capacitance due to packaging 2.4.4 external clock the external clock (ec) mode allows an externally generated logic level clock to be used as the systems clock source. when operating in this mode, the external clock source is connected to the osc1 allowing osc2 to be configured as an i/o or as clkout. the clkout function is selected by the fosc bits of the config1h configuration register. when osc2 is configured as clkout, the frequency at the pin is the frequency of the ec oscillator divided by 4. three different power settings are available for ec mode. the power settings allow for a reduced i dd of the device, if the ec clock is known to be in a specific range. if there is an expected range of frequencies for the ec clock, select the power mode for the highest frequency. ec low power 0 C 250 khz ec medium power 250 khz C 4 mhz ec high power 4 C 64 mhz 2.5 secondary external oscillator the secondary external oscillator is designed to drive an external 32.768 khz crystal. this oscillator is enabled or disabled by the t1oscen bit of the t1con register. see section 10.0 ?timer1 module? for more information. note 1: a series resistor (r s ) may be required for ceramic resonators with low drive level. 2: the value of r f varies with the oscillator mode selected (typically between 2 m ? to 10 m ?? . 3: an additional parallel feedback resistor (r p ) may be required for proper ceramic resonator operation. c1 c2 ceramic r s (1) osc1/clkin r f (2) sleep to internal logic pic ? mcu r p (3) resonator osc2/clkout osc2/clkout (1) c ext r ext pic ? mcu osc1/clkin f osc /4 or internal clock v dd v ss recommended values: 10 k ? ? r ext ? 100 k ? c ext > 20 pf note 1: alternate pin functions are listed in section 1.0 ?device overview? . 2: output depends upon rc or rcio clock mode. i/o (2) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 16 ? 2009-2016 microchip technology inc. 2.6 internal oscillator the internal oscillator module contains two independent oscillators which are: lfintosc: low-frequency internal oscillator hfintosc: high-frequency internal oscillator when operating with either oscillator, osc1 will be an i/o and osc2 will be either an i/o or clkout. the clkout function is selected by the fosc bits of the config1h configuration register. when osc2 is configured as clkout, the frequency at the pin is the frequency of the internal oscillator divided by 4. 2.6.1 lfintosc the low-frequency internal oscillator (lfintosc) is a 31 khz internal clock source. the lfintosc oscillator is the clock source for: power-up timer watchdog timer fail-safe clock monitor the lfintosc is enabled when any of the following conditions are true: power-up timer is enabled (pwrten = 0 ) watchdog timer is enabled (wdten = 1 ) watchdog timer is enabled by software (wdten = 0 and swdten = 1 ) fail-safe clock monitor is enabled (fcmem = 1 ) scs1= 1 and ircf<2:0> = 000 and intsrc = 0 fosc<3:0> selects the internal oscillator as the primary clock and ircf<2:0> = 000 and intsrc = 0 ieso = 1 (two-speed start-up) and ircf<2:0> = 000 and intsrc = 0 2.6.2 hfintosc the high-frequency internal oscillator (hfintosc) is a precision oscillator that is factory-calibrated to operate at 16 mhz. the output of the hfintosc connects to a postscaler and a multiplexer (see figure 2-1 ). one of eight frequencies can be selected using the ircf<2:0> bits of the osccon register. the following frequencies are available from the hfintosc: 16 mhz 8 mhz 4 mhz 2 mhz 1 mhz (default after reset) 500 khz 250 khz 31 khz the hfiofs bit of the osccon register indicates whether the hfintosc is stable. the hfintosc is enabled if any of the following conditions are true: scs1= 1 and ircf<2:0> ? 000 scs1= 1 and ircf<2:0> = 000 and intsrc = 1 fosc<3:0> selects the internal oscillator as the primary clock and - ircf<2:0> ? 000 or - ircf<2:0> = 000 and intsrc = 1 ieso = 1 (two-speed start-up) and - ircf<2:0> ? 000 or - ircf<2:0> = 000 and intsrc = 1 fcmem= 1 (fail-safe clock monitoring) and - ircf<2:0> ? 000 or - ircf<2:0> = 000 and intsrc = 1 note 1: selecting 31 khz from the hfintosc oscillator requires ircf<2:0> = 000 and the intsrc bit of the osctune register to be set. if the intsrc bit is clear, the system clock will come from the lfintosc. 2: additional adjustments to the frequency of the hfintosc can made via the osctune registers. see register 2-3 for more details. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 17 pic18(l)f1xk22 2.7 oscillator control the oscillator control (osccon) ( register 2-1 ) and the oscillator control 2 (osccon2) ( register 2-2 ) registers control the system clock and frequency selection options. register 2-1: osccon: os cillator control register r/w-0 r/w-0 r/w-1 r/w-1 r-q r-0 r/w-0 r/w-0 idlen ircf2 ircf1 ircf0 osts (1) hfiofs scs1 scs0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 q = depends on condition -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 idlen: idle enable bit 1 = device enters idle mode on sleep instruction 0 = device enters sleep mode on sleep instruction bit 6-4 ircf<2:0>: internal oscillator frequency select bits 111 = 16 mhz 110 = 8 mhz 101 = 4 mhz 100 = 2 mhz 011 = 1 mhz (3) 010 = 500 khz 001 = 250 khz 000 = 31 khz (2) bit 3 osts: oscillator start-up time-out status bit (1) 1 = device is running from the clock defined by fosc<2:0> of the config1 regi ster 0 = device is running from the internal oscillator (hfintosc or lfintosc ) bit 2 hfiofs: hfintosc frequency stable bit 1 = hfintosc frequency is stable 0 = hfintosc frequency is not stable bit 1-0 scs<1:0>: system clock select bits 1x = internal oscillator block 01 = secondary (timer1) oscillator 00 = primary clock (determined by config1h[fosc<3:0>]). note 1: reset state depends on state of the ieso configuration bit. 2: source selected by the intsrc bit of the osctune register, see text. 3: default output frequency of hfintosc on reset. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 18 ? 2009-2016 microchip technology inc. register 2-2: osccon2: osci llator control register 2 u-0 u-0 u-0 u-0 u-0 r/w-1 r/w-0 r-x pri_sd hfiofl lfiofs bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 q = depends on condition -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-3 unimplemented: read as 0 bit 2 pri_sd: primary oscillator drive circuit shutdown bit 1 = oscillator drive circuit on 0 = oscillator drive circuit off (zero power) bit 1 hfiofl: hfintosc frequency locked bit 1 = hfintosc is in lock 0 = hfintosc has not yet locked bit 0 lfiofs: lfintosc frequency stable bit 1 = lfintosc is stable 0 = lfintosc is not stable downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 19 pic18(l)f1xk22 2.7.1 osctune register the hfintosc is factory-calibrated, but can be adjusted in software by writing to the tun<5:0> bits of the osctune register ( register 2-3 ). the default value of the tun<5:0> is 000000 . the value is a 6-bit twos complement number. when the osctune register is modified, the hfintosc frequency will begin shifting to the new frequency. code execution continues during this shift, while giving no indication that the shift has occurred. osctune does not affect the lfintosc frequency. the operation of features that depend on the lfintosc clock source frequency, such as the power-up timer (pwrt), watchdog timer (wdt), fail-safe clock monitor (fscm) and peripherals, are not affected by the change in frequency. the osctune register also implements the intsrc and pllen bits, which control certain features of the internal oscillator block. the intsrc bit allows users to select which internal oscillator provides the clock source when the 31 khz frequency option is selected. this is covered in greater detail in section 2.6.1 ?lfintosc? . the pllen bit controls the operation of the frequency multiplier. for more details about the function of the pllen bit see section 2.10 ?4x phase lock loop frequency multiplier? . register 2-3: osctune: osci llator tuning register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 intsrc pllen tun5 tun4 tun3 tun2 tun1 tun0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 intsrc: internal oscillator low-frequency source select bit 1 = 31.25 khz device clock derived from 16 mhz hfintosc source (divide-by-512 enabled) 0 = 31 khz device clock derived directly from lfintosc internal oscillator bit 6 pllen: frequency multiplier pll bit 1 = pll enabled (for hfintosc 8 mhz and 16 mhz only) 0 = pll disabled bit 5-0 tun<5:0>: frequency tuning bits 011111 = maximum frequency 011110 = 000001 = 000000 = oscillator module is running at the factory-calibrated frequency. 111111 = 100000 = minimum frequency downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 20 ? 2009-2016 microchip technology inc. 2.8 oscillator start-up timer the primary external oscillator, when configured for lp, xt or hs modes, incorporates an oscillator start-up timer (ost). the ost ensures that the oscillator starts and provides a stable clock to the oscillator module. the ost times out when 1024 oscillations on osc1 have occurred. during the ost period, with the system clock set to the primary external oscillator, the program counter does not increment suspending program execution. the ost period will occur following: power-on reset (por) brown-out reset (bor) wake-up from sleep oscillator being enabled expiration of power-up timer (pwrt) in order to minimize latency between external oscillator start-up and code execution, the two-speed start-up mode can be selected. see section 2.11 ?two-speed start-up mode? for more information. 2.9 clock switching the device contains circuitry to prevent clock glitches due to a change of the system clock source. to accomplish this, a short pause in the system clock occurs during the clock switch. if the new clock source is not stable (e.g., ost is active), the device will continue to execute from the old clock source until the new clock source becomes stable. the timing of a clock switch is as follows: 1. scs<1:0> bits of the osccon register are modified. 2. the system clock will continue to operate from the old clock until the new clock is ready. 3. clock switch circuitry waits for two consecutive rising edges of the old clock after the new clock is ready. 4. the system clock is held low, starting at the next falling edge of the old clock. 5. clock switch circuitry waits for an additional two rising edges of the new clock. 6. on the next falling edge of the new clock, the low hold on the system clock is release and the new clock is switched in as the system clock. 7. clock switch is complete. refer to figure 2-5 for more details. figure 2-5: clock switch timing old clock new clock ircf <2:0> system clock start-up time (1) clock sync running high speed low speed select old select new new clk ready low speed high speed old clock new clock ircf <2:0> system clock start-up time (1) clock sync running select old select new new clk ready note 1: start-up time includes t ost (1024 t osc ) for external clocks, plus t pll (approx. 2 ms) for hspll mode. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 21 pic18(l)f1xk22 table 2-2: examples of dela ys due to clock switching 2.10 4x phase lock loop frequency multiplier a phase locked loop (pll) circuit is provided as an option for users who wish to use a lower-frequency external oscillator or to operate at 32 mhz or 64 mhz with the hfintosc. the pll is designed for an input frequency from 4 mhz to 16 mhz. the pll multiplies its input frequency by a factor of four when the pll is enabled. this may be useful for customers who are concerned with emi, due to high-frequency crystals. two bits control the pll: the pll_en bit of the config1h configuration register and the pllen bit of the osctune register. the pll is enabled when the pll_en bit is set and it is under software control when the pll_en bit is cleared. refer to ta b l e 2 - 3 and table 2-4 for more information. table 2-3: pll configuration 2.11 two-speed start-up mode two-speed start-up mode provides additional power savings by minimizing the latency between external oscillator start-up timer (ost) and code execution. in applications that make heavy use of the sleep mode, two-speed start-up will remove the ost period, which can reduce the overall power consumption of the device. two-speed start-up mode is enabled by setting the ieso bit of the config1h configuration register. with two-speed start-up enabled, the device will execute instructions using the internal oscillator during the primary external oscillator ost period. when the system clock is set to the primary external oscillator and the oscillator is configured for lp, xt or hs modes, the device will not execute code during the ost period. the ost will suspend program execution until 1024 oscillations are counted. two-speed start-up mode minimizes the delay in code execution by operating from the internal oscillator while the ost is active. the system clock will switch back to the primary external oscillator after the ost period has expired. two-speed start-up will become active after: power-on reset (por) power-up timer (pwrt), if enabled wake-up from sleep the osts bit of the osccon register reports which oscillator the device is currently using for operation. the device is running from the oscillator defined by the fosc bits of the config1h configuration register when the osts bit is set. the device is running from the internal oscillator when the osts bit is clear. switch from switch to oscillator delay sleep/por lfintosc hfintosc oscillator warm-up delay (t warm ) sleep/por lp, xt, hs 1024 clock cycles sleep/por ec, rc 8 clock cycles pll_en pllen pll status 1x pll enabled 01 pll enabled 00 pll disabled table 2-4: pll config1h/software enable clock source restrictions mode pll config1h enable (pll_en) pll software enable (pllen) lp yes no xt yes no hs yes no ec yes no extrc yes no lf intosc no no hf intosc 8/16 mhz 8/16 mhz downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 22 ? 2009-2016 microchip technology inc. 2.12 fail-safe clock monitor the fail-safe clock monitor (fscm) allows the device to continue operating should the external oscillator fail. the fscm can detect oscillator failure any time after the oscillator start-up timer (ost) has expired. the fscm is enabled by setting the fcmen bit in the config1h configuration register. the fscm is applicable to all external oscillator modes (lp, xt, hs, ec and rc). figure 2-6: fscm block diagram 2.12.1 fail-safe detection the fscm module detects a failed oscillator by comparing the external oscillator to the fscm sample clock. the sample clock is generated by dividing the lfintosc by 64. see figure 2-6 . inside the fail detector block is a latch. the external clock sets the latch on each falling edge of the external clock. the sample clock clears the latch on each rising edge of the sample clock. a failure is detected when an entire half- cycle of the sample clock elapses before the primary clock goes low. 2.12.2 fail-safe operation when the external clock fails, the fscm switches the device clock to an internal clock source and sets the bit flag oscfif of the pir2 register. the oscfif flag will generate an interrupt if the oscfie bit of the pie2 register is also set. the device firmware can then take steps to mitigate the problems that may arise from a failed clock. the system clock will continue to be sourced from the internal clock source until the device firmware successfully restarts the external oscillator and switches back to external operation. an automatic transition back to the failed clock source will not occur. the internal clock source chosen by the fscm is determined by the ircf<2:0> bits of the osccon register. this allows the internal oscillator to be configured before a failure occurs. 2.12.3 fail-safe condition clearing the fail-safe condition is cleared by either one of the following: any reset by toggling the scs1 bit of the osccon register both of these conditions restart the ost. while the ost is running, the device continues to operate from the intosc selected in osccon. when the ost times out, the fail-safe condition is cleared and the device automatically switches over to the external clock source. the fail-safe condition need not be cleared before the oscfif flag is cleared. 2.12.4 reset or wake-up from sleep the fscm is designed to detect an oscillator failure after the oscillator start-up timer (ost) has expired. the ost is used after waking up from sleep and after any type of reset. the ost is not used with the ec or rc clock modes so that the fscm will be active as soon as the reset or wake-up has completed. when the fscm is enabled, the two-speed start-up is also enabled. therefore, the device will always be executing code while the ost is operating. external lfintosc 64 s r q 31 khz (~32 ? s) 488 hz (~2 ms) clock monitor latch clock failure detected oscillator clock q sample clock note: due to the wide range of oscillator start-up times, the fail-safe circuit is not active during oscillator start-up (i.e., after exiting reset or sleep). after an appropriate amount of time, the user should check the osts bit of the osccon register to verify the oscillator start-up and that the system clock switchover has successfully completed. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 23 pic18(l)f1xk22 figure 2-7: fscm timing diagram table 2-5: summary of registers asso ciated with clock sources name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page config1h ieso fcmen pclken pll_en fosc3 fosc2 fosc1 fosc0 251 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 osccon idlen ircf2 ircf1 ircf0 osts hfiofs scs1 scs0 246 osccon2 pri_sd hfiofl lfiofs 246 osctune intsrc pllen tun5 tun4 tun3 tun2 tun1 tun0 248 ipr2 oscfip c1ip c2ip eeip bclip tmr3ip 248 pie2 oscfie c1ie c2ie eeie bclie tmr3ie 248 pir2 oscfif c1if c2if eeif bclif tmr3if 248 t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 246 legend: x = unknown, u = unchanged, C = unimplemented locations read as 0 . shaded cells are not used by oscillators. note 1: other (non power-up) resets include mclr reset and watchdog timer reset during normal operation. oscfif system clock output sample clock failure detected oscillator failure note: the system clock is normally at a much higher frequency than the sample clock. the relative frequencies in this example have been chosen for clarity. (q) te s t test test clock monitor output downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 24 ? 2009-2016 microchip technology inc. 3.0 memory organization there are three types of memory in pic18 enhanced microcontroller devices: program memory data ram data eeprom as harvard architecture devices, the data and program memories use separate busses; this allows for concurrent access of the two memory spaces. the data eeprom, for practical purposes, can be regarded as a peripheral device, since it is addressed and accessed through a set of control registers. additional detailed information on the operation of the flash program memory is provided in section 4.0 ?flash program memory? . data eeprom is discussed separately in section 5.0 ?data eeprom memory? . 3.1 program memory organization pic18 microcontrollers implement a 21-bit program counter, which is capable of addressing a 2-mbyte program memory (pc) space. accessing a location between the upper boundary of the physically implemented memory and the 2-mbyte address will return all 0 s (a nop instruction). this family of devices contain the following: pic18(l)f13k22: 8 kbytes of flash memory, up to 4,096 single-word instructions pic18(l)f14k22: 16 kbytes of flash memory, up to 8,192 single-word instructions pic18 devices have two interrupt vectors and one reset vector. the reset vector address is at 0000h and the interrupt vector addresses are at 0008h and 0018h. the program memory map for pic18(l)f1xk22 devices is shown in figure 3-1 . memory block details are shown in figure 3-2 . figure 3-1: program memory map and stack for pic18(l)f1xk22 devices pc<20:0> stack level 1 ? stack level 31 reset vector low priority interrupt vector ?? call,rcall,return retfie,retlw 21 0000h0018h high priority interrupt vector 0008h user memory space 1fffffh 4000h 3fffh 200000h on-chip program memory read ? 0 ? 1fffh 2000h on-chip program memory read ? 0 ? pic18(l)f14k22 pic18(l)f13k22 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 25 pic18(l)f1xk22 3.1.1 program counter the program counter (pc) specifies the address of the instruction to fetch for execution. the pc is 21-bit wide and is contained in three separate 8-bit registers. the low byte, known as the pcl register, is both readable and writable. the high byte, or pch register, contains the pc<15:8> bits; it is not directly readable or writable. updates to the pch register are performed through the pclath register. the upper byte is called pcu. this register contains the pc<20:16> bits; it is also not directly readable or writable. updates to the pcu register are performed through the pclatu register. the contents of pclath and pclatu are transferred to the program counter by any operation that writes pcl. similarly, the upper two bytes of the program counter are transferred to pclath and pclatu by an operation that reads pcl. this is useful for computed offsets to the pc (see section 3.1.4.1 ?computed goto? ). the pc addresses bytes in the program memory. to prevent the pc from becoming misaligned with word instructions, the least significant bit (lsb) of pcl is fixed to a value of 0 . the pc increments by 2 to address sequential instructions in the program memory. the call , rcall , goto and program branch instructions write to the program counter directly. for these instructions, the contents of pclath and pclatu are not transferred to the program counter. 3.1.2 return address stack the return address stack allows any combination of up to 31 program calls and interrupts to occur. the pc is pushed onto the stack when a call or rcall instruction is executed or an interrupt is acknowledged. the pc value is pulled off the stack on a return, retlw or a retfie instruction. pclatu and pclath are not affected by any of the return or call instructions. the stack operates as a 31-word by 21-bit ram and a 5-bit stack pointer, stkptr. the stack space is not part of either program or data space. the stack pointer is readable and writable and the address on the top of the stack is readable and writable through the top-of- stack (tos) special file registers. data can also be pushed to, or popped from the stack, using these registers. a call type instruction causes a push onto the stack; the stack pointer is first incremented and the location pointed to by the stack pointer is written with the contents of the pc (already pointing to the instruction following the call ). a return type instruction causes a pop from the stack; the contents of the location pointed to by the stkptr are transferred to the pc and then the stack pointer is decremented. the stack pointer is initialized to 00000 after all resets. there is no ram associated with the location corresponding to a stack pointer value of 00000 ; this is only a reset value. status bits indicate if the stack is full or has overflowed or has underflowed. 3.1.2.1 top-of-stack access only the top of the return address stack (tos) is readable and writable. a set of three registers, tosu:tosh:tosl, hold the contents of the stack location pointed to by the stkptr register ( figure 3-2 ). this allows users to implement a software stack if necessary. after a call, rcall or interrupt, the software can read the pushed value by reading the tosu:tosh:tosl registers. these values can be placed on a user defined software stack. at return time, the software can return these values to tosu:tosh:tosl and do a return. the user must disable the global interrupt enable bits while accessing the stack to prevent inadvertent stack corruption. figure 3-2: return address stack and associated registers 00011 001a34h 11111 11110 11101 00010 00001 00000 00010 return address stack <20:0> to p - o f - st a c k 000d58h tosl tosh tosu 34h 1ah 00h stkptr<4:0> top-of-stack registers stack pointer downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 26 ? 2009-2016 microchip technology inc. 3.1.2.2 return stack pointer (stkptr) the stkptr register ( figure 3-1 ) contains the stack pointer value, the stkful (stack full) bit and the stkunf (stack underflow) bits. the value of the stack pointer can be 0 through 31. the stack pointer increments before values are pushed onto the stack and decrements after values are popped off the stack. on reset, the stack pointer value will be zero. the user may read and write the stack pointer value. this feature can be used by a real-time operating system (rtos) for return stack maintenance. after the pc is pushed onto the stack 31 times (without popping any values off the stack), the stkful bit is set. the stkovf bit is cleared by software or by a por. the action that takes place when the stack becomes full depends on the state of the stvren (stack overflow reset enable) configuration bit. (refer to section 23.1 ?configuration bits? for a description of the device configuration bits.) if stvren is set (default), the 31st push will push the (pc + 2) value onto the stack, set the stkovf bit and reset the device. the stkovf bit will remain set and the stack pointer will be set to zero. if stvren is cleared, the stkovf bit will be set on the 31st push and the stack pointer will increment to 31. any additional pushes will not overwrite the 31st push and stkptr will remain at 31. when the stack has been popped enough times to unload the stack, the next pop will return a value of zero to the pc and sets the stkunf bit, while the stack pointer remains at zero. the stkunf bit will remain set until cleared by software or until a por occurs. 3.1.2.3 push and pop instructions since the top-of-stack is readable and writable, the ability to push values onto the stack and pull values off the stack without disturbing normal program execution is a desirable feature. the pic18 instruction set includes two instructions, push and pop , that permit the tos to be manipulated under software control. tosu, tosh and tosl can be modified to place data or a return address on the stack. the push instruction places the current pc value onto the stack. this increments the stack pointer and loads the current pc value onto the stack. the pop instruction discards the current tos by decrementing the stack pointer. the previous value pushed onto the stack then becomes the tos value. note: returning a value of zero to the pc on an underflow has the effect of vectoring the program to the reset vector, where the stack conditions can be verified and appropriate actions can be taken. this is not the same as a reset, as the contents of the sfrs are not affected. register 3-1: stkptr: stack pointer register r/c-0 r/c-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 stkovf (1) stkunf (1) sp4 sp3 sp2 sp1 sp0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented c = clearable only bit -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 stkovf: stack overflow flag bit (1) 1 = stack became full or overflowed 0 = stack has not become full or overflowed bit 6 stkunf: stack underflow flag bit (1) 1 = stack underflow occurred 0 = stack underflow did not occur bit 5 unimplemented: read as 0 bit 4-0 sp<4:0>: stack pointer location bits note 1: bit 7 and bit 6 are cleared by user software or by a por. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 27 pic18(l)f1xk22 3.1.2.4 stack overflow and underflow resets device resets on stack overflow and stack underflow conditions are enabled by setting the stvren bit in configuration register 4l. when stvren is set, a full or underflow will set the appropriate stkovf or stkunf bit and then cause a device reset. when stvren is cleared, a full or underflow condition will set the appropriate stkovf or stkunf bit but not cause a device reset. the stkovf or stkunf bits are cleared by the user software or a power-on reset. 3.1.3 fast register stack a fast register stack is provided for the status, wreg and bsr registers, to provide a fast return option for interrupts. the stack for each register is only one level deep and is neither readable nor writable. it is loaded with the current value of the corresponding register when the processor vectors for an interrupt. all interrupt sources will push values into the stack registers. the values in the registers are then loaded back into their associated registers if the retfie, fast instruction is used to return from the interrupt. if both low and high priority interrupts are enabled, the stack registers cannot be used reliably to return from low priority interrupts. if a high priority interrupt occurs while servicing a low priority interrupt, the stack register values stored by the low priority interrupt will be overwritten. in these cases, users must save the key registers by software during a low priority interrupt. if interrupt priority is not used, all interrupts may use the fast register stack for returns from interrupt. if no interrupts are used, the fast register stack can be used to restore the status, wreg and bsr registers at the end of a subroutine call. to use the fast register stack for a subroutine call, a call label , fast instruction must be executed to save the status, wreg and bsr registers to the fast register stack. a return , fast instruction is then executed to restore these registers from the fast register stack. example 3-1 shows a source code example that uses the fast register stack during a subroutine call and return. example 3-1: fast register stack code example 3.1.4 look-up tables in program memory there may be programming situations that require the creation of data structures, or look-up tables, in program memory. for pic18 devices, look-up tables can be implemented in two ways: computed goto table reads 3.1.4.1 computed goto a computed goto is accomplished by adding an offset to the program counter. an example is shown in example 3-2 . a look-up table can be formed with an addwf pcl instruction and a group of retlw nn instructions. the w register is loaded with an offset into the table before executing a call to that table. the first instruction of the called routine is the addwf pcl instruction. the next instruction executed will be one of the retlw nn instructions that returns the value nn to the calling function. the offset value (in wreg) specifies the number of bytes that the program counter should advance and should be multiples of 2 (lsb = 0 ). in this method, only one data byte may be stored in each instruction location and room on the return address stack is required. example 3-2: computed goto using an offset value 3.1.4.2 table reads and table writes a better method of storing data in program memory allows two bytes of data to be stored in each instruction location. look-up table data may be stored two bytes per program word by using table reads and writes. the table pointer (tblptr) register specifies the byte address and the table latch (tablat) register contains the data that is read from or written to program memory. data is transferred to or from program memory one byte at a time. table read and table write operations are discussed further in section 4.1 ?table reads and table writes? . call sub1, fast ;status, wreg, bsr ;saved in fast register ;stack ?? sub1 ?? return, fast ;restore values saved ;in fast register stack movf offset, w call table org nn00h table addwf pcl retlw nnh retlw nnh retlw nnh . . . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 28 ? 2009-2016 microchip technology inc. 3.2 pic18 instruction cycle 3.2.1 clocking scheme the microcontroller clock input, whether from an internal or external source, is internally divided by four to generate four non-overlapping quadrature clocks (q1, q2, q3 and q4). internally, the program counter is incremented on every q1; the instruction is fetched from the program memory and latched into the instruction register during q4. the instruction is decoded and executed during the following q1 through q4. the clocks and instruction execution flow are shown in figure 3-3 . 3.2.2 instruction flow/pipelining an instruction cycle consists of four q cycles: q1 through q4. the instruction fetch and execute are pipelined in such a manner that a fetch takes one instruction cycle, while the decode and execute take another instruction cycle. however, due to the pipelining, each instruction effectively executes in one cycle. if an instruction causes the program counter to change (e.g., goto ), then two cycles are required to complete the instruction ( example 3-3 ). a fetch cycle begins with the program counter (pc) incrementing in q1. in the execution cycle, the fetched instruction is latched into the instruction register (ir) in cycle q1. this instruction is then decoded and executed during the q2, q3 and q4 cycles. data memory is read during q2 (operand read) and written during q4 (destination write). figure 3-3: clock/ instruction cycle example 3-3: instruction pipeline flow q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 osc1 q1q2 q3 q4 pc osc2/clkout (rc mode) pc pc + 2 pc + 4 fetch inst (pc) execute inst (pc C 2) fetch inst (pc + 2) execute inst (pc) fetch inst (pc + 4) execute inst (pc + 2) internal phase clock all instructions are single cycle, except for any program branches. these take two cycles sin ce the fetch instruction is flushed from the pipeline while the new instruction is being fetched and then executed. t cy 0t cy 1t cy 2t cy 3t cy 4t cy 5 1. movlw 55h fetch 1 execute 1 2. movwf portb fetch 2 execute 2 3. bra sub_1 fetch 3 execute 3 4. bsf porta, bit3 (forced nop) fetch 4 flush ( nop ) 5. instruction @ address sub_1 fetch sub_1 execute sub_1 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 29 pic18(l)f1xk22 3.2.3 instructions in program memory the program memory is addressed in bytes. instructions are stored as either two bytes or four bytes in program memory. the least significant byte (lsb) of an instruction word is always stored in a program memory location with an even address (lsb = 0 ). to maintain alignment with instruction boundaries, the pc increments in steps of 2 and the lsb will always read 0 (see section 3.1.1 ?program counter? ). figure 3-4 shows an example of how instruction words are stored in the program memory. the call and goto instructions have the absolute program memory address embedded into the instruction. since instructions are always stored on word boundaries, the data contained in the instruction is a word address. the word address is written to pc<20:1>, which accesses the desired byte address in program memory. instruction #2 in figure 3-4 shows how the instruction goto 0006h is encoded in the program memory. program branch instructions, which encode a relative address offset, operate in the same manner. the offset value stored in a branch instruction represents the number of single-word instructions that the pc will be offset by. section 24.0 ?instruction set summary? provides further details of the instruction set. figure 3-4: instructions in program memory 3.2.4 two-word instructions the standard pic18 instruction set has four two-word instructions: call , movff , goto and lsfr . in all cases, the second word of the instruction always has 1111 as its four most significant bits (msb); the other 12 bits are literal data, usually a data memory address. the use of 1111 in the 4 msbs of an instruction specifies a special form of nop . if the instruction is executed in proper sequence C immediately after the first word C the data in the second word is accessed and used by the instruction sequence. if the first word is skipped for some reason and the second word is executed by itself, a nop is executed instead. this is necessary for cases when the two-word instruction is preceded by a conditional instruction that changes the pc. example 3-4 shows how this works. example 3-4: two- word instructions word address lsb = 1 lsb = 0 ? program memory byte locations ? ? 000000h000002h 000004h 000006h instruction 1: movlw 055h 0fh 55h 000008h instruction 2: goto 0006h efh 03h 00000ah f0h 00h 00000ch instruction 3: movff 123h, 456h c1h 23h 00000eh f4h 56h 000010h 000012h000014h note: see section 3.6 ?pic18 instruction execution and the extended instruc- tion set? for information on two-word instructions in the extended instruction set. case 1: object code source code 0110 0110 0000 0000 tstfsz reg1 ; is ram location 0? 1100 0001 0010 0011 movff reg1, reg2 ; no, skip this word 1111 0100 0101 0110 ; execute this word as a nop 0010 0100 0000 0000 addwf reg3 ; continue code case 2: object code source code 0110 0110 0000 0000 tstfsz reg1 ; is ram location 0? 1100 0001 0010 0011 movff reg1, reg2 ; yes, execute this word 1111 0100 0101 0110 ; 2nd word of instruction 0010 0100 0000 0000 addwf reg3 ; continue code downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 30 ? 2009-2016 microchip technology inc. 3.3 data memory organization the data memory in pic18 devices is implemented as static ram. each register in the data memory has a 12-bit address, allowing up to 4096 bytes of data memory. the memory space is divided into as many as 16 banks that contain 256 bytes each. figure 3-5 and figure 3-6 show the data memory organization for the pic18(l)f1xk22 devices. the data memory contains special function registers (sfrs) and general purpose registers (gprs). the sfrs are used for control and status of the controller and peripheral functions, while gprs are used for data storage and scratchpad operations in the users application. any read of an unimplemented location will read as 0 s. the instruction set and architecture allow operations across all banks. the entire data memory may be accessed by direct, indirect or indexed addressing modes. addressing modes are discussed later in this subsection. to ensure that commonly used registers (sfrs and select gprs) can be accessed in a single cycle, pic18 devices implement an access bank. this is a 256-byte memory space that provides fast access to sfrs and the lower portion of gpr bank 0 without using the bank select register (bsr). section 3.3.2 ?access bank? provides a detailed description of the access ram. 3.3.1 bank select register (bsr) large areas of data memory require an efficient addressing scheme to make rapid access to any address possible. ideally, this means that an entire address does not need to be provided for each read or write operation. for pic18 devices, this is accomplished with a ram banking scheme. this divides the memory space into 16 contiguous banks of 256 bytes. depending on the instruction, each location can be addressed directly by its full 12-bit address, or an 8-bit low-order address and a 4-bit bank pointer. most instructions in the pic18 instruction set make use of the bank pointer, known as the bank select register (bsr). this sfr holds the 4 most significant bits of a locations address; the instruction itself includes the 8 least significant bits. only the four lower bits of the bsr are implemented (bsr<3:0>). the upper four bits are unused; they will always read 0 and cannot be written to. the bsr can be loaded directly by using the movlb instruction. the value of the bsr indicates the bank in data memory; the 8 bits in the instruction show the location in the bank and can be thought of as an offset from the banks lower boundary. the relationship between the bsrs value and the bank division in data memory is shown in figure 3-5 and figure 3-6 . since up to 16 registers may share the same low-order address, the user must always be careful to ensure that the proper bank is selected before performing a data read or write. for example, writing what should be program data to an 8-bit address of f9h while the bsr is 0fh will end up resetting the program counter. while any bank can be selected, only those banks that are actually implemented can be read or written to. writes to unimplemented banks are ignored, while reads from unimplemented banks will return 0 s. even so, the status register will still be affected as if the operation was successful. the data memory maps in figure 3-5 and figure 3-6 indicate which banks are implemented. in the core pic18 instruction set, only the movff instruction fully specifies the 12-bit address of the source and target registers. this instruction ignores the bsr completely when it executes. all other instructions include only the low-order address as an operand and must use either the bsr or the access bank to locate their target registers. note: the operation of some aspects of data memory are changed when the pic18 extended instruction set is enabled. see section 3.5 ?data memory and the extended instruction set? for more information. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 31 pic18(l)f1xk22 figure 3-5: data memory map for pic18(l)f13k22 devices bank 0 bank 1 bank 14 bank 15 data memory map bsr<3:0> = 0000 = 0001 = 1111 060h 05fh f60h fffh 00h 5fh 60h ffh access bank when a = 0 : the bsr is ignored and the access bank is used. the first 96 bytes are general purpose ram (from bank 0). the second 160 bytes are special function registers (from bank 15). when a = 1 : the bsr specifies the bank used by the instruction. f5fh f00h effh 1ffh 100h 0ffh 000h access ram ffh 00h ffh 00h ffh 00h gpr sfr access ram high access ram low bank 2 = 0110 = 0010 (sfrs) 2ffh 200h 3ffh 300h 4ffh 400h 5ffh 500h 6ffh 600h 7ffh 700h 8ffh 800h 9ffh 900h affh a00h bffh b00h cffh c00h dffh d00h e00h bank 3 bank 4 bank 5 bank 6 bank 7 bank 8 bank 9 bank 10 bank 11 bank 12 bank 13 ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h = 0011 = 0100 = 0101 = 0111 = 1000 = 1001 = 1010 = 1011 = 1100 = 1101 = 1110 unused read 00h unused f53h sfr (1) note 1: sfrs occupying f53h to f5fh address space are not in the virtual bank. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 32 ? 2009-2016 microchip technology inc. figure 3-6: data memory map for pic18(l)f14k22 devices bank 0 bank 1 bank 14 bank 15 data memory map bsr<3:0> = 0000 = 0001 = 1111 060h 05fh 00h 5fh 60h ffh access bank when a = 0 : the bsr is ignored and the access bank is used. the first 96 bytes are general purpose ram (from bank 0). the second 160 bytes are special function registers (from bank 15). when a = 1 : the bsr specifies the bank used by the instruction. f00h effh 1ffh 100h 0ffh 000h access ram ffh 00h ffh 00h gprgpr access ram high access ram low bank 2 = 0110 = 0010 (sfrs) 2ffh 200h 3ffh 300h 4ffh 400h 5ffh 500h 6ffh 600h 7ffh 700h 8ffh 800h 9ffh 900h affh a00h bffh b00h cffh c00h dffh d00h e00h bank 3 bank 4 bank 5 bank 6 bank 7 bank 8 bank 9 bank 10 bank 11 bank 12 bank 13 ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h = 0011 = 0100 = 0101 = 0111 = 1000 = 1001 = 1010 = 1011 = 1100 = 1101 = 1110 unused read 00h note 1: sfrs occupying f53h to f5fh address space are not in the virtual bank. f60h fffh f5fh ffh 00h sfr unused f53h sfr (1) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 33 pic18(l)f1xk22 figure 3-7: use of the bank select register (direct addressing) note 1: the access ram bit of the instruction can be used to force an override of the selected bank (bsr<3:0>) to the registers of the access bank. 2: the movff instruction embeds the entire 12-bit address in the instruction. data memory bank select (2) 7 0 from opcode (2) 0000 000h100h 200h 300h f00h e00h fffh bank 0 bank 1 bank 2 bank 14 bank 15 00hffh 00h ffh 00h ffh 00h ffh 00h ffh 00hffh bank 3 through bank 13 0010 11111111 7 0 bsr (1) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 34 ? 2009-2016 microchip technology inc. 3.3.2 access bank while the use of the bsr with an embedded 8-bit address allows users to address the entire range of data memory, it also means that the user must always ensure that the correct bank is selected. otherwise, data may be read from or written to the wrong location. this can be disastrous if a gpr is the intended target of an operation, but an sfr is written to instead. verifying and/or changing the bsr for each read or write to data memory can become very inefficient. to streamline access for the most commonly used data memory locations, the data memory is configured with an access bank, which allows users to access a mapped block of memory without specifying a bsr. the access bank consists of the first 96 bytes of memory (00h-5fh) in bank 0 and the last 160 bytes of memory (60h-ffh) in block 15. the lower half is known as the access ram and is composed of gprs. this upper half is also where the devices sfrs are mapped. these two areas are mapped contiguously in the access bank and can be addressed in a linear fashion by an 8-bit address ( figure 3-5 and figure 3-6 ). the access bank is used by core pic18 instructions that include the access ram bit (the a parameter in the instruction). when a is equal to 1 , the instruction uses the bsr and the 8-bit address included in the opcode for the data memory address. when a is 0 , however, the instruction is forced to use the access bank address map; the current value of the bsr is ignored entirely. using this forced addressing allows the instruction to operate on a data address in a single cycle, without updating the bsr first. for 8-bit addresses of 60h and above, this means that users can evaluate and operate on sfrs more efficiently. the access ram below 60h is a good place for data values that the user might need to access rapidly, such as immediate computational results or common program variables. access ram also allows for faster and more code efficient context saving and switching of variables. the mapping of the access bank is slightly different when the extended instruction set is enabled (xinst configuration bit = 1 ). this is discussed in more detail in section 3.5.3 ?mapping the access bank in indexed literal offset mode? . 3.3.3 general purpose register file pic18 devices may have banked memory in the gpr area. this is data ram, which is available for use by all instructions. gprs start at the bottom of bank 0 (address 000h) and grow upwards towards the bottom of the sfr area. gprs are not initialized by a power-on reset and are unchanged on all other resets. 3.3.4 special function registers the special function registers (sfrs) are registers used by the cpu and peripheral modules for controlling the desired operation of the device. these registers are implemented as static ram. sfrs start at the top of data memory (fffh) and extend downward to occupy the top portion of bank 15 (f60h to fffh). a list of these registers is given in ta b l e 3 - 1 and tab l e 3 - 2 . the sfrs can be classified into two sets: those associated with the core device functionality (alu, resets and interrupts) and those related to the peripheral functions. the reset and interrupt registers are described in their respective chapters, while the alus status register is described later in this section. registers related to the operation of a peripheral feature are described in the chapter for that peripheral. the sfrs are typically distributed among the peripherals whose functions they control. unused sfr locations are unimplemented and read as 0 s. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 35 pic18(l)f1xk22 table 3-1: special function register map for pic18(l)f1xk22 devices address name address name address name address name address name fffh tosu fd7h tmr0h fafh spbrg f87h (2) f5fh (2) ffeh tosh fd6h tmr0l faeh rcreg f86h (2) f5eh (2) ffdh tosl fd5h t0con fadh txreg f85h (2) f5dh (2) ffch stkptr fd4h (2) fach txsta f84h (2) f5ch (2) ffbh pclatu fd3h osccon fabh rcsta f83h (2) f5bh (2) ffah pclath fd2h osccon2 faah (2) f82h portc f5ah (2) ff9h pcl fd1h wdtcon fa9h eeadr f81h portb f59h (2) ff8h tblptru fd0h rcon fa8h eedata f80h porta f58h (2) ff7h tblptrh fcfh tmr1h fa7h eecon2 (1) f7fh anselh f57h (2) ff6h tblptrl fceh tmr1l fa6h eecon1 f7eh ansel f56h (2) ff5h tablat fcdh t1con fa5h (2) f7dh (2) f55h (2) ff4h prodh fcch tmr2 fa4h (2) f7ch (2) f54h (2) ff3h prodl fcbh pr2 fa3h (2) f7bh (2) f53h (2) ff2h intcon fcah t2con fa2h ipr2 f7ah iocb ff1h intcon2 fc9h sspbuf fa1h pir2 f79h ioca ff0h intcon3 fc8h sspadd fa0h pie2 f78h wpub fefh indf0 (1) fc7h sspstat f9fh ipr1 f77h wpua feeh postinc0 (1) fc6h sspcon1 f9eh pir1 f76h slrcon fedh postdec0 (1) fc5h sspcon2 f9dh pie1 f75h (2) fech preinc0 (1) fc4h adresh f9ch (2) f74h (2) febh plusw0 (1) fc3h adresl f9bh osctune f73h (2) feah fsr0h fc2h adcon0 f9ah (2) f72h (2) fe9h fsr0l fc1h adcon1 f99h (2) f71h (2) fe8h wreg fc0h adcon2 f98h (2) f70h (2) fe7h indf1 (1) fbfh ccpr1h f97h (2) f6fh sspmask fe6h postinc1 (1) fbeh ccpr1l f96h (2) f6eh (2) fe5h postdec1 (1) fbdh ccp1con f95h (2) f6dh cm1con0 fe4h preinc1 (1) fbch vrefcon2 f94h trisc f6ch cm2con1 fe3h plusw1 (1) fbbh vrefcon1 f93h trisb f6bh cm2con0 fe2h fsr1h fbah vrefcon0 f92h trisa f6ah (2) fe1h fsr1l fb9h pstrcon f91h (2) f69h srcon1 fe0h bsr fb8h baudcon f90h (2) f68h srcon0 fdfh indf2 (1) fb7h pwm1con f8fh (2) f67h (2) fdeh postinc2 (1) fb6h eccp1as f8eh (2) f66h (2) fddh postdec2 (1) fb5h (2) f8dh (2) f65h (2) fdch preinc2 (1) fb4h (2) f8ch (2) f64h (2) fdbh plusw2 (1) fb3h tmr3h f8bh latc f63h (2) fdah fsr2h fb2h tmr3l f8ah latb f62h (2) fd9h fsr2l fb1h t3con f89h lata f61h (2) fd8h status fb0h spbrgh f88h (2) f60h (2) legend: = unimplemented data memory locations, read as 0 , note 1: this is not a physical register. 2: unimplemented registers are read as 0 . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 36 ? 2009-2016 microchip technology inc. table 3-2: register file summary (pic18(l)f1xk22) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor details on page: tosu top-of-stack upper byte (tos<20:16>) ---0 0000 245, 25 tosh top-of-stack, high byte (tos<15:8>) 0000 0000 245, 25 tosl top-of-stack, low byte (tos<7:0>) 0000 0000 245, 25 stkptr stkovf stkunf sp4 sp3 sp2 sp1 sp0 00-0 0000 245, 26 pclatu holding register for pc<20:16> ---0 0000 245, 25 pclath holding register for pc<15:8> 0000 0000 245, 25 pcl pc, low byte (pc<7:0>) 0000 0000 245, 25 tblptru program memory table pointer upper byte (tblptr<20:16>) ---0 0000 245, 48 tblptrh program memory table pointer, high byte (tblptr<15:8>) 0000 0000 245, 48 tblptrl program memory table pointer, low byte (tblptr<7:0>) 0000 0000 245, 48 tablat program memory table latch 0000 0000 245, 48 prodh product register, high byte xxxx xxxx 245, 58 prodl product register, low byte xxxx xxxx 245, 58 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 0000 000x 245, 62 intcon2 rabpu intedg0 intedg1 intedg2 t m r 0 i p rabip 1111 -1-1 245, 63 intcon3 int2ip int1ip int2ie int1ie int2if int1if 11-0 0-00 245, 64 indf0 uses contents of fsr0 to address data memory C value of fsr0 not changed (not a physical regist er) n/a 245, 41 postinc0 uses contents of fsr0 to address data memory C value of fsr0 post-incremented (not a ph ysical register) n/a 245, 41 postdec0 uses contents of fsr0 to address data memory C value of fsr0 post-decremented (not a physi cal register) n/a 245, 41 preinc0 uses contents of fsr0 to address data memory C value of fsr0 pre-incremented (not a physical register) n/a 245, 41 plusw0 uses contents of fsr0 to address data memory C value of fsr0 pre-incremented (not a physical register ) C value of fsr0 offset by w n/a 245, 41 fsr0h indirect data memory address pointer 0, high byte ---- 0000 245, 41 fsr0l indirect data memory address pointer 0, low byte xxxx xxxx 245, 41 wreg working register xxxx xxxx 245 indf1 uses contents of fsr1 to address data memory C value of fsr1 not changed (not a physical regist er) n/a 245, 41 postinc1 uses contents of fsr1 to address data memory C value of fsr1 post-incremented (not a ph ysical register) n/a 245, 41 postdec1 uses contents of fsr1 to address data memory C value of fsr1 post-decremented (not a physi cal register) n/a 245, 41 preinc1 uses contents of fsr1 to address data memory C value of fsr1 pre-incremented (not a physical register) n/a 245, 41 plusw1 uses contents of fsr1 to address data memory C value of fsr1 pre-incremented (not a physical register ) C value of fsr1 offset by w n/a 245, 41 fsr1h indirect data memory address pointer 1, high byte ---- 0000 246, 41 fsr1l indirect data memory address pointer 1, low byte xxxx xxxx 246, 41 bsr bank select register ---- 0000 246, 30 indf2 uses contents of fsr2 to address data memory C value of fsr2 not changed (not a physical regist er) n/a 246, 41 postinc2 uses contents of fsr2 to address data memory C value of fsr2 post-incremented (not a ph ysical register) n/a 246, 41 postdec2 uses contents of fsr2 to address data memory C value of fsr2 post-decremented (not a physi cal register) n/a 246, 41 preinc2 uses contents of fsr2 to address data memory C value of fsr2 pre-incremented (not a physical register) n/a 246, 41 plusw2 uses contents of fsr2 to address data memory C value of fsr2 pre-incremented (not a physical register ) C value of fsr2 offset by w n/a 246, 41 fsr2h indirect data memory address pointer 2, high byte ---- 0000 246, 41 fsr2l indirect data memory address pointer 2, low byte xxxx xxxx 246, 41 status n o v z d cc ---x xxxx 246, 39 legend: x = unknown, u = unchanged, = unimplemented, q = value depends on condition note 1: the sboren bit is only available when the boren<1:0> configuration bits = 01 ; otherwise it is disabled and reads as 0 . see section 22.4 ?brown-out reset (bor)? . 2: the ra3 bit is only available when master clear reset is disabled (mclre configuration bit = 0 ). otherwise, ra3 reads as 0 . this bit is read-only. 3: unimplemented, read as 1 . downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 37 pic18(l)f1xk22 tmr0h timer0 register, high byte 0000 0000 246, 92 tmr0l timer0 register, low byte xxxx xxxx 246, 92 t0con tmr0on t08bit t0cs t0se psa t0ps2 t0ps1 t0ps0 1111 1111 246, 91 osccon idlen ircf2 ircf1 ircf0 osts hfiofs scs1 scs0 0011 qq00 246, 17 osccon2 pri_sd hfiofl lfiofs ---- -10x 246, 18 wdtcon s w d t e n --- ---0 246, 260 rcon ipen sboren (1) r i to pd por bor 0q-1 11q0 237, 246, 71 tmr1h timer1 register, high byte xxxx xxxx 246, 94 tmr1l timer1 register, low bytes xxxx xxxx 246, 94 t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 0000 0000 246, 94 tmr2 timer2 register 0000 0000 246, 100 pr2 timer2 period register 1111 1111 246, 100 t2con t2outps3 t2outps2 t2outps1 t2outps0 tmr2on t2ckps1 t2ckps0 -000 0000 246, 100 sspbuf ssp receive buffer/transmit register xxxx xxxx 246, 128, 130 sspadd ssp address register in i 2 c slave mode. ssp baud rate reload register in i 2 c master mode. 0000 0000 246, 147 sspstat smp cke d/a psr / w ua bf 0000 0000 246, 128, 137 sspcon1 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 0000 0000 246, 128, 138 sspcon2 gcen ackstat ackdt acken rcen pen rsen sen 0000 0000 246, 139 adresh a/d result register, high byte xxxx xxxx 247, 197 adresl a/d result register, low byte xxxx xxxx 247, 197 adcon0 chs3 chs2 chs1 chs0 go/done adon --00 0000 247, 203 adcon1 pvcfg1 pvcfg0 nvcfg1 nvcfg0 ---- 0000 247, 204 adcon2 adfm acqt2 acqt1 acqt0 adcs2 adcs1 adcs0 0-00 0000 247, 205 ccpr1h capture/compare/pwm register 1, high byte xxxx xxxx 247, 126 ccpr1l capture/compare/pwm register 1, low byte xxxx xxxx 247, 126 ccp1con p1m1 p1m0 dc1b1 dc1b0 ccp1m3 ccp1m2 ccp1m1 ccp1m0 0000 0000 247, 106 vrefcon2 dac1r<4:0> ---0 0000 247, 236 vrefcon1 d1en d1lps dac1oe --- d1pss<1:0> d1nss 000- 00-0 247, 235 vrefcon0 fvr1en fvr1st fvr1s<1:0> 0001 ---- 247, 232 pstrcon strsync strd strc strb stra ---0 0001 247, 123 baudcon abdovf rcidl dtrxp cktxp brg16 wue abden 0100 0-00 247, 181 pwm1con prsen pdc6 pdc5 pdc4 pdc3 pdc2 pdc1 pdc0 0000 0000 247, 122 eccp1as eccpase eccpas2 eccpas1 eccpas0 pssac1 pssac0 pssbd1 pssbd0 0000 0000 247, 118 tmr3h timer3 register, high byte xxxx xxxx 247, 102 tmr3l timer3 register, low byte xxxx xxxx 247, 102 t3con rd16 t3ckps1 t3ckps0 t3ccp1 t3sync tmr3cs tmr3on 0-00 0000 247, 102 table 3-2: register file summary (pic18(l)f1xk22) (continued) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor details on page: legend: x = unknown, u = unchanged, = unimplemented, q = value depends on condition note 1: the sboren bit is only available when the boren<1:0> configuration bits = 01 ; otherwise it is disabled and reads as 0 . see section 22.4 ?brown-out reset (bor)? . 2: the ra3 bit is only available when master clear reset is disabled (mclre configuration bit = 0 ). otherwise, ra3 reads as 0 . this bit is read-only. 3: unimplemented, read as 1 . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 38 ? 2009-2016 microchip technology inc. spbrgh eusart baud rate generator register, high byte 0000 0000 247, 182 spbrg eusart baud rate generator register, low byte 0000 0000 247, 182 rcreg eusart receive register 0000 0000 247, 175 txreg eusart transmit register 0000 0000 247, 172 txsta csrc tx9 txen sync sendb brgh trmt tx9d 0000 0010 247, 179 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 000x 247, 180 eeadr eeadr7 eeadr6 eeadr5 eeadr4 eeadr3 eeadr2 eeadr1 eeadr0 0000 0000 247, 45, 54 eedata eeprom data register 0000 0000 247, 45, 54 eecon2 eeprom control register 2 (not a physical register) 0000 0000 247, 45, 54 eecon1 eepgd cfgs free wrerr wren wr rd xx-0 x000 247, 45, 54 ipr2 oscfip c1ip c2ip eeip bclip t m r 3 i p 1111 1-1- 248, 70 pir2 oscfif c1if c2if eeif bclif t m r 3 i f 0000 0-0- 248, 66 pie2 oscfie c1ie c2ie eeie bclie t m r 3 i e 0000 0-0- 248, 68 ipr1 adip rcip txip sspip ccp1ip tmr2ip tmr1ip -111 1111 248, 69 pir1 adif rcif txif sspif ccp1if tmr2if tmr1if -000 0000 248, 65 pie1 adie rcie txie sspie ccp1ie tmr2ie tmr1ie -000 0000 248, 67 osctune intsrc pllen tun5 tun4 tun3 tun2 tun1 tun0 0000 0000 248, 19 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 1111 1111 248, 84 trisb trisb7 trisb6 trisb5 trisb4 1111 ---- 248, 80 trisa trisa5 trisa4 (3) trisa2 trisa1 trisa0 --11 1111 248, 75 latc latc7 latc6 latc5 latc4 latc3 latc2 latc1 latc0 xxxx xxxx 248, 85 latb latb7 latb6 latb5 latb4 xxxx ---- 248, 80 lata l a t a 5l a t a 4 lata2 lata1 lata0 --xx -xxx 248, 76 portc rc7 rc6 rc5 rc4 rc3 rc2 rc1 rc0 xxxx xxxx 248, 84 portb rb7 rb6 rb5 rb4 xxxx ---- 248, 80 porta ra5 ra4 ra3 (2) ra2 ra1 ra0 --xx xxxx 248, 75 anselh ans11 ans10 ans9 ans8 ---- 1111 248, 89 ansel ans7 ans6 ans5 ans4 ans3 ans2 ans1 ans0 1111 1111 248, 88 iocb iocb7 iocb6 iocb5 iocb4 0000 ---- 248, 81 ioca ioca5 ioca4 ioca3 ioca2 ioca1 ioca0 --00 0000 248, 76 wpub wpub7 wpub6 wpub5 wpub4 1111 ---- 248, 81 wpua wpua5 wpua4 wpua3 wpua2 wpua1 wpua0 --11 1111 245, 76 slrcon slrc slrb slra ---- -111 248, 90 sspmsk msk7 msk6 msk5 msk4 msk3 msk2 msk1 msk0 1111 1111 248, 146 cm1con0 c1on c1out c1oe c1pol c1sp c1r c1ch1 c1ch0 0000 0000 248, 216 cm2con1 mc1out mc2out c1rsel c2rsel c1hys c2hys c1sync c2sync 0000 0000 248, 220 cm2con0 c2on c2out c2oe c2pol c2sp c2r c2ch1 c2ch0 0000 0000 248, 217 srcon1 srspe srscke srsc2e srsc1e srrpe srrcke srrc2e srrc1e 0000 0000 248, 230 srcon0 srlen srclk2 srclk1 srclk0 srqen srnqen srps srpr 0000 0000 248, 229 table 3-2: register file summary (pic18(l)f1xk22) (continued) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor details on page: legend: x = unknown, u = unchanged, = unimplemented, q = value depends on condition note 1: the sboren bit is only available when the boren<1:0> configuration bits = 01 ; otherwise it is disabled and reads as 0 . see section 22.4 ?brown-out reset (bor)? . 2: the ra3 bit is only available when master clear reset is disabled (mclre configuration bit = 0 ). otherwise, ra3 reads as 0 . this bit is read-only. 3: unimplemented, read as 1 . downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 39 pic18(l)f1xk22 3.3.5 status register the status register, shown in register 3-2 , contains the arithmetic status of the alu. as with any other sfr, it can be the operand for any instruction. if the status register is the destination for an instruction that affects the z, dc, c, ov or n bits, the results of the instruction are not written; instead, the status register is updated according to the instruction performed. therefore, the result of an instruction with the status register as its destination may be different than intended. as an example, clrf status will set the z bit and leave the remaining status bits unchanged ( 000u u1uu ). it is recommended that only bcf , bsf , swapf , movff and movwf instructions are used to alter the status register, because these instructions do not affect the z, c, dc, ov or n bits in the status register. for other instructions that do not affect status bits, see the instruction set summaries in table 24-2 and table 24-3 . note: the c and dc bits operate as the borrow and digit borrow bits, respectively, in subtraction. register 3-2: status: status register u-0 u-0 u-0 r/w-x r/w-x r/w-x r/w-x r/w-x no v z d c (1) c (1) bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-5 unimplemented: read as 0 bit 4 n: negative bit this bit is used for signed arithmetic (twos complement). it indicates whether the result was negative (alu msb = 1 ). 1 = result was negative 0 = result was positive bit 3 ov: overflow bit this bit is used for signed arithmetic (twos complement). it indicates an overflow of the 7-bit magnitude which causes the sign bit (bit 7 of the result) to change state. 1 = overflow occurred for signed arithmetic (in this arithmetic operation) 0 = no overflow occurred bit 2 z: zero bit 1 = the result of an arithmetic or logic operation is zero 0 = the result of an arithmetic or logic operation is not zero bit 1 dc: digit carry/borrow bit ( addwf , addlw,sublw,subwf instructions) (1) 1 = a carry-out from the 4th low-order bit of the result occurred 0 = no carry-out from the 4th low-order bit of the result bit 0 c: carry/borrow bit ( addwf , addlw, sublw, subwf instructions) (1) 1 = a carry-out from the most significant bit of the result occurred 0 = no carry-out from the most significant bit of the result occurred note 1: for borrow , the polarity is reversed. a subtraction is executed by adding the twos complement of the second operand. for rotate ( rrf , rlf ) instructions, this bit is loaded with either the high-order or low-order bit of the source register. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 40 ? 2009-2016 microchip technology inc. 3.4 data addressing modes while the program memory can be addressed in only one way C through the program counter C information in the data memory space can be addressed in several ways. for most instructions, the addressing mode is fixed. other instructions may use up to three modes, depending on which operands are used and whether or not the extended instruction set is enabled. the addressing modes are: inherent literal direct indirect an additional addressing mode, indexed literal offset, is available when the extended instruction set is enabled (xinst configuration bit = 1 ). its operation is discussed in greater detail in section 3.5.1 ?indexed addressing with literal offset? . 3.4.1 inherent and literal addressing many pic18 control instructions do not need any argument at all; they either perform an operation that globally affects the device or they operate implicitly on one register. this addressing mode is known as inherent addressing. examples include sleep , reset and daw . other instructions work in a similar way but require an additional explicit argument in the opcode. this is known as literal addressing mode because they require some literal value as an argument. examples include addlw and movlw , which respectively, add or move a literal value to the w register. other examples include call and goto , which include a 20-bit program memory address. 3.4.2 direct addressing direct addressing specifies all or part of the source and/or destination address of the operation within the opcode itself. the options are specified by the arguments accompanying the instruction. in the core pic18 instruction set, bit-oriented and byte- oriented instructions use some version of direct addressing by default. all of these instructions include some 8-bit literal address as their least significant byte. this address specifies either a register address in one of the banks of data ram ( section 3.3.3 ?general purpose register file? ) or a location in the access bank ( section 3.3.2 ?access bank? ) as the data source for the instruction. the access ram bit a determines how the address is interpreted. when a is 1 , the contents of the bsr ( section 3.3.1 ?bank select register (bsr)? ) are used with the address to determine the complete 12-bit address of the register. when a is 0 , the address is interpreted as being a register in the access bank. addressing that uses the access ram is sometimes also known as direct forced addressing mode. a few instructions, such as movff , include the entire 12-bit address (either source or destination) in their opcodes. in these cases, the bsr is ignored entirely. the destination of the operations results is determined by the destination bit d. when d is 1 , the results are stored back in the source register, overwriting its original contents. when d is 0 , the results are stored in the w register. instructions without the d argument have a destination that is implicit in the instruction; their destination is either the target register being operated on or the w register. 3.4.3 indirect addressing indirect addressing allows the user to access a location in data memory without giving a fixed address in the instruction. this is done by using file select registers (fsrs) as pointers to the locations which are to be read or written. since the fsrs are themselves located in ram as special file registers, they can also be directly manipulated under program control. this makes fsrs very useful in implementing data structures, such as tables and arrays in data memory. the registers for indirect addressing are also implemented with indirect file operands (indfs) that permit automatic manipulation of the pointer value with auto-incrementing, auto-decrementing or offsetting with another value. this allows for efficient code, using loops, such as the example of clearing an entire ram bank in example 3-5 . example 3-5: how to clear ram (bank 1) using indirect addressing note: the execution of some instructions in the core pic18 instruction set are changed when the pic18 extended instruction set is enabled. see section 3.5 ?data memory and the extended instruction set? fo r more information. lfsr fsr0, 100h ; next clrf postinc0 ; clear indf ; register then ; inc pointer btfss fsr0h, 1 ; all done with ; bank1? bra next ; no, clear next continue ; yes, continue downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 41 pic18(l)f1xk22 3.4.3.1 fsr registers and the indf operand at the core of indirect addressing are three sets of registers: fsr0, fsr1 and fsr2. each represents a pair of 8-bit registers, fsrnh and fsrnl. each fsr pair holds a 12-bit value, therefore the four upper bits of the fsrnh register are not used. the 12-bit fsr value can address the entire range of the data memory in a linear fashion. the fsr register pairs, then, serve as pointers to data memory locations. indirect addressing is accomplished with a set of indirect file operands, indf0 through indf2. these can be thought of as virtual registers: they are mapped in the sfr space but are not physically implemented. reading or writing to a particular indf register actually accesses its corresponding fsr register pair. a read from indf1, for example, reads the data at the address indicated by fsr1h:fsr1l. instructions that use the indf registers as operands actually use the contents of their corresponding fsr as a pointer to the instructions target. the indf operand is just a convenient way of using the pointer. because indirect addressing uses a full 12-bit address, data ram banking is not necessary. thus, the current contents of the bsr and the access ram bit have no effect on determining the target address. 3.4.3.2 fsr registers and postinc, postdec, preinc and plusw in addition to the indf operand, each fsr register pair also has four additional indirect operands. like indf, these are virtual registers which cannot be directly read or written. accessing these registers actually accesses the location to which the associated fsr register pair points, and also performs a specific action on the fsr value. they are: postdec: accesses the location to which the fsr points, then automatically decrements the fsr by 1 afterwards postinc: accesses the location to which the fsr points, then automatically increments the fsr by 1 afterwards preinc: automatically increments the fsr by 1, then uses the location to which the fsr points in the operation plusw: adds the signed value of the w register (range of -127 to 128) to that of the fsr and uses the location to which the result points in the operation. in this context, accessing an indf register uses the value in the associated fsr register without changing it. similarly, accessing a plusw register gives the fsr value an offset by that in the w register; however, neither w nor the fsr is actually changed in the operation. accessing the other virtual registers changes the value of the fsr register. figure 3-8: indirect addressing fsr1h:fsr1l 0 7 data memory 000h100h 200h 300h f00h e00h fffh bank 0 bank 1 bank 2 bank 14bank 15 bank 3 through bank 13 addwf, indf1, 1 0 7 using an instruction with one of the indirect addressing registers as the operand.... ...uses the 12-bit address stored in the fsr pair associated with that register.... ...to determine the data memory location to be used in that operation. in this case, the fsr1 pair contains ecch. this means the contents of location ecch will be added to that of the w register and stored back in ecch. xxxx 1110 11001100 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 42 ? 2009-2016 microchip technology inc. operations on the fsrs with postdec, postinc and preinc affect the entire register pair; that is, rollovers of the fsrnl register from ffh to 00h carry over to the fsrnh register. on the other hand, results of these operations do not change the value of any flags in the status register (e.g., z, n, ov, etc.). the plusw register can be used to implement a form of indexed addressing in the data memory space. by manipulating the value in the w register, users can reach addresses that are fixed offsets from pointer addresses. in some applications, this can be used to implement some powerful program control structure, such as software stacks, inside of data memory. 3.4.3.3 operations by fsrs on fsrs indirect addressing operations that target other fsrs or virtual registers represent special cases. for example, using an fsr to point to one of the virtual registers will not result in successful operations. as a specific case, assume that fsr0h:fsr0l contains fe7h, the address of indf1. attempts to read the value of the indf1 using indf0 as an operand will return 00h. attempts to write to indf1 using indf0 as the operand will result in a nop . on the other hand, using the virtual registers to write to an fsr pair may not occur as planned. in these cases, the value will be written to the fsr pair but without any incrementing or decrementing. thus, writing to either the indf2 or postdec2 register will write the same value to the fsr2h:fsr2l. since the fsrs are physical registers mapped in the sfr space, they can be manipulated through all direct operations. users should proceed cautiously when working on these registers, particularly if their code uses indirect addressing. similarly, operations by indirect addressing are generally permitted on all other sfrs. users should exercise the appropriate caution that they do not inadvertently change settings that might affect the operation of the device. 3.5 data memory and the extended instruction set enabling the pic18 extended instruction set (xinst configuration bit = 1 ) significantly changes certain aspects of data memory and its addressing. specifically, the use of the access bank for many of the core pic18 instructions is different; this is due to the introduction of a new addressing mode for the data memory space. what does not change is just as important. the size of the data memory space is unchanged, as well as its linear addressing. the sfr map remains the same. core pic18 instructions can still operate in both direct and indirect addressing mode; inherent and literal instructions do not change at all. indirect addressing with fsr0 and fsr1 also remain unchanged. 3.5.1 indexed addressing with literal offset enabling the pic18 extended instruction set changes the behavior of indirect addressing using the fsr2 register pair within access ram. under the proper conditions, instructions that use the access bank C that is, most bit-oriented and byte-oriented instructions C can invoke a form of indexed addressing using an offset specified in the instruction. this special addressing mode is known as indexed addressing with literal offset, or indexed literal offset mode. when using the extended instruction set, this addressing mode requires the following: the use of the access bank is forced (a = 0 ) and the file address argument is less than or equal to 5fh. under these conditions, the file address of the instruction is not interpreted as the lower byte of an address (used with the bsr in direct addressing), or as an 8-bit address in the access bank. instead, the value is interpreted as an offset value to an address pointer, specified by fsr2. the offset and the contents of fsr2 are added to obtain the target address of the operation. 3.5.2 instructions affected by indexed literal offset mode any of the core pic18 instructions that can use direct addressing are potentially affected by the indexed literal offset addressing mode. this includes all byte-oriented and bit-oriented instructions, or almost one-half of the standard pic18 instruction set. instructions that only use inherent or literal addressing modes are unaffected. additionally, byte-oriented and bit-oriented instructions are not affected if they do not use the access bank (access ram bit is 1 ), or include a file address of 60h or above. instructions meeting these criteria will continue to execute as before. a comparison of the different possible addressing modes when the extended instruction set is enabled is shown in figure 3-9 . those who desire to use byte-oriented or bit-oriented instructions in the indexed literal offset mode should note the changes to assembler syntax for this mode. this is described in more detail in section 24.2.1 ?extended instruction syntax? . downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 43 pic18(l)f1xk22 figure 3-9: comparing addressing options for bit-oriented and byte-oriented instructions (extended instruction set enabled) example instruction: addwf, f, d, a (opcode: 0010 01da ffff ffff ) when ?a? = 0 and f ? 60h: the instruction executes in direct forced mode. f is interpreted as a location in the access ram between 060h and 0ffh. this is the same as locations f60h to fffh (bank 15) of data memory. locations below 60h are not available in this addressing mode. when ?a? = 0 and f ??? 5fh: the instruction executes in indexed literal offset mode. f is interpreted as an offset to the address value in fsr2. the two are added together to obtain the address of the target register for the instruction. the address can be anywhere in the data memory space. note that in this mode, the correct syntax is now: addwf [k], d where k is the same as f. when ?a? = 1 (all values of f): the instruction executes in direct mode (also known as direct long mode). f is interpreted as a location in one of the 16 banks of the data memory space. the bank is designated by the bank select register (bsr). the address can be in any implemented bank in the data memory space. 000h 060h 100h f00h f60h fffh valid range 00h60h ffh data memory access ram bank 0 bank 1 through bank 14 bank 15 sfrs 000h 060h 100h f00h f60h fffh data memory bank 0 bank 1 through bank 14 bank 15 sfrs fsr2h fsr2l ffffffff 001001da ffffffff 001001da 000h 060h 100h f00h f60h fffh data memory bank 0 bank 1 through bank 14 bank 15 sfrs for f bsr 00000000 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 44 ? 2009-2016 microchip technology inc. 3.5.3 mapping the access bank in indexed literal offset mode the use of indexed literal offset addressing mode effectively changes how the first 96 locations of access ram (00h to 5fh) are mapped. rather than containing just the contents of the bottom section of bank 0, this mode maps the contents from a user defined window that can be located anywhere in the data memory space. the value of fsr2 establishes the lower boundary of the addresses mapped into the window, while the upper boundary is defined by fsr2 plus 95 (5fh). addresses in the access ram above 5fh are mapped as previously described (see section 3.3.2 ?access bank? ). an example of access bank remapping in this addressing mode is shown in figure 3-10 . remapping of the access bank applies only to operations using the indexed literal offset mode. operations that use the bsr (access ram bit is 1 ) will continue to use direct addressing as before. 3.6 pic18 instruction execution and the extended instruction set enabling the extended instruction set adds eight additional commands to the existing pic18 instruction set. these instructions are executed as described in section 24.2 ?extended instruction set? . figure 3-10: remapping the access bank with indexed literal offset addressing data memory 000h100h 200h f60h f00h fffh bank 1 bank 15 bank 2 through bank 14 sfrs addwf f, d, a fsr2h:fsr2l = 120h locations in the region from the fsr2 pointer (120h) to the pointer plus 05fh (17fh) are mapped to the bottom of the access ram (000h-05fh). special file registers at f60h through fffh are mapped to 60h through ffh, as usual. bank 0 addresses below 5fh can still be addressed by using the bsr. access bank 00h60h ffh sfrs bank 1 ?window? bank 0 window example situation: 120h 17fh 5fh bank 1 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 45 pic18(l)f1xk22 4.0 flash program memory the flash program memory is readable, writable and erasable during normal operation over the entire v dd range. a read from program memory is executed one byte at a time. a write to program memory is executed on blocks of 16 or 8 bytes at a time depending on the specific device (see tab l e 4 - 1 ). program memory is erased in blocks of 64 bytes at a time. the difference between the write and erase block sizes requires from 4 to 8 block writes to restore the contents of a single block erase. a bulk erase operation can not be issued from user code. table 4-1: write/erase block sizes writing or erasing program memory will cease instruction fetches until the operation is complete. the program memory cannot be accessed during the write or erase, therefore, code cannot execute. an internal programming timer terminates program memory writes and erases. a value written to program memory does not need to be a valid instruction. executing a program memory location that forms an invalid instruction results in a nop . 4.1 table reads and table writes in order to read and write program memory, there are two operations that allow the processor to move bytes between the program memory space and the data ram: table read ( tblrd ) table write ( tblwt ) the program memory space is 16-bit wide, while the data ram space is 8-bit wide. table reads and table writes move data between these two memory spaces through an 8-bit register (tablat). the table read operation retrieves one byte of data directly from program memory and places it into the tablat register. figure 4-1 shows the operation of a table read. the table write operation stores one byte of data from the tablat register into a write block holding register. the procedure to write the contents of the holding registers into program memory is detailed in section 4.5 ?writing to flash program memory? . figure 4-2 shows the operation of a table write with program memory and data ram. table operations work with byte entities. tables containing data, rather than program instructions, are not required to be word-aligned. therefore, a table can start and end at any byte address. if a table write is being used to write executable code into program memory, program instructions will need to be word-aligned. figure 4-1: tabl e read operation device write block size (bytes) erase block size (bytes) pic18(l)f13k22 8 64 pic18(l)f14k22 16 64 table pointer (1) table latch (8-bit) program memory tblptrh tblptrl tablat tblptru instruction: tblrd * note 1: table pointer register points to a byte in program memory. program memory (tblptr) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 46 ? 2009-2016 microchip technology inc. figure 4-2: table write operation 4.2 control registers several control registers are used in conjunction with the tblrd and tblwt instructions. these include the: eecon1 register eecon2 register tablat register tblptr registers 4.2.1 eecon1 and eecon2 registers the eecon1 register ( register 4-1 ) is the control register for memory accesses. the eecon2 register is not a physical register; it is used exclusively in the memory write and erase sequences. reading eecon2 will read all 0 s. the eepgd control bit determines if the access will be a program or data eeprom memory access. when eepgd is clear, any subsequent operations will operate on the data eeprom memory. when eepgd is set, any subsequent operations will operate on the program memory. the cfgs control bit determines if the access will be to the configuration/calibration registers or to program memory/data eeprom memory. when cfgs is set, subsequent operations will operate on configuration registers regardless of eepgd (see section 23.0 ?special features of the cpu? ). when cfgs is clear, memory selection access is determined by eepgd. the free bit allows the program memory erase operation. when free is set, an erase operation is initiated on the next wr command. when free is clear, only writes are enabled. the wren bit, when set, will allow a write operation. the wren bit is clear on power-up. the wrerr bit is set by hardware when the wr bit is set and cleared when the internal programming timer expires and the write operation is complete. the wr control bit initiates write operations. the wr bit cannot be cleared, only set, by firmware. then wr bit is cleared by hardware at the completion of the write operation. table pointer (1) table latch (8-bit) tblptrh tblptrl tablat program memory (tblptr) tblptru instruction: tblwt * note 1: during table writes the table pointer does not point directly to program memory. the lsbs of tblprtl actually point to an address within the write block holding registers. the msbs of the table pointer determine where the write block will eventually be writt en. the process for writing the holding registers to the program memory array is discussed in section 4.5 ?writing to flash program memory? . holding registers program memory note: during normal operation, the wrerr is read as 1 . this can indicate that a write operation was prematurely terminated by a reset, or a write operation was attempted improperly. note: the eeif interrupt flag bit of the pir2 register is set when the write is complete. the eeif flag stays set until cleared by firmware. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 47 pic18(l)f1xk22 register 4-1: eecon1: data eeprom control 1 register r/w-x r/w-x u-0 r/w-0 r/w-x r/w-0 r/s-0 r/s-0 eepgd cfgs free wrerr wren wr rd bit 7 bit 0 legend: r = readable bit w = writable bit s = bit can be set by software, but not cleared u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 eepgd: flash program or data eeprom memory select bit 1 = access flash program memory 0 = access data eeprom memory bit 6 cfgs: flash program/data eeprom or configuration select bit 1 = access configuration registers 0 = access flash program or data eeprom memory bit 5 unimplemented: read as 0 bit 4 free: flash row (block) erase enable bit 1 = erase the program memory block addressed by tblptr on the next wr command (cleared by completion of erase operation) 0 = perform write-only bit 3 wrerr: flash program/data eeprom error flag bit (1) 1 = a write operation is prematurely terminated (any reset during self-timed programming in normal operation, or an improper write attempt) 0 = the write operation completed bit 2 wren: flash program/data eeprom write enable bit 1 = allows write cycles to flash program/data eeprom 0 = inhibits write cycles to flash program/data eeprom bit 1 wr: write control bit 1 = initiates a data eeprom erase/write cycle or a program memory erase cycle or write cycle. (the operation is self-timed and the bit is cleared by hardware once write is complete. the wr bit can only be set (not cleared) by software.) 0 = write cycle to the eeprom is complete bit 0 rd: read control bit 1 = initiates an eeprom read (read takes one cycle. rd is cleared by hardware. the rd bit can only be set (not cleared) by software. rd bit cannot be set when eepgd = 1 or cfgs = 1 .) 0 = does not initiate an eeprom read note 1: when a wrerr occurs, the eepgd and cfgs bits are not cleared. this allows tracing of the error condition. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 48 ? 2009-2016 microchip technology inc. 4.2.2 tablat C table latch register the table latch (tablat) is an 8-bit register mapped into the sfr space. the table latch register is used to hold 8-bit data during data transfers between program memory and data ram. 4.2.3 tblptr C table pointer register the table pointer (tblptr) register addresses a byte within the program memory. the tblptr is comprised of three sfr registers: table pointer upper byte, table pointer high byte and table pointer low byte (tblptru:tblptrh:tblptrl). these three registers join to form a 22-bit wide pointer. the low- order 21 bits allow the device to address up to 2 mbytes of program memory space. the 22nd bit allows access to the device id, the user id and the configuration bits. the table pointer register, tblptr, is used by the tblrd and tblwt instructions. these instructions can update the tblptr in one of four ways based on the table operation. these operations are shown in table 4-2 . these operations on the tblptr affect only the low-order 21 bits. 4.2.4 table pointer boundaries tblptr is used in reads, writes and erases of the flash program memory. when a tblrd is executed, all 22 bits of the tblptr determine which byte is read from program memory directly into the tablat register. when a tblwt is executed the byte in the tablat register is written, not to flash memory but, to a holding register in preparation for a program memory write. the holding registers constitute a write block which varies depending on the device (see tab l e 4 - 1 ).the 3, 4, or 5 lsbs of the tblptrl register determine which specific address within the holding register block is written to. the msbs of the table pointer have no effect during tblwt operations. when a program memory write is executed the entire holding register block is written to the flash memory at the address determined by the msbs of the tblptr. the 3, 4, or 5 lsbs are ignored during flash memory writes. for more detail, see section 4.5 ?writing to flash program memory? . when an erase of program memory is executed, the 16 msbs of the table pointer register (tblptr<21:6>) point to the 64-byte block that will be erased. the least significant bits (tblptr<5:0>) are ignored. figure 4-3 describes the relevant boundaries of tblptr based on flash program memory operations. table 4-2: table pointer operations with tblrd and tblwt instructions figure 4-3: table pointer bo undaries based on operation example operation on table pointer tblrd* tblwt* tblptr is not modified tblrd*+ tblwt*+ tblptr is incremented after the read/write tblrd*- tblwt*- tblptr is decremented after the read/write tblrd+* tblwt+* tblptr is incremented before the read/write 21 16 15 87 0 table erase/write table write table read ? tblptr<21:0> tblptrl tblptrh tblptru tblptr (1) tblptr<21:n+1> (1) note 1: n = 3, 4, 5, or 6 for block sizes of 8, 16, 32 or 64 bytes, respectively. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 49 pic18(l)f1xk22 4.3 reading the flash program memory the tblrd instruction retrieves data from program memory and places it into data ram. table reads from program memory are performed one byte at a time. tblptr points to a byte address in program space. executing tblrd places the byte pointed to into tablat. in addition, tblptr can be modified automatically for the next table read operation. the internal program memory is typically organized by words. the least significant bit of the address selects between the high and low bytes of the word. figure 4-4 shows the interface between the internal program memory and the tablat. figure 4-4: reads from flash program memory example 4-1: reading a flash program memory word (even byte address) program memory (odd byte address) tblrd tablat tblptr = xxxxx1 fetch instruction register (ir) read register tblptr = xxxxx0 movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the word movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl read_word tblrd*+ ; read into tablat and increment movf tablat, w ; get data movwf word_even tblrd*+ ; read into tablat and increment movfw tablat, w ; get data movf word_odd downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 50 ? 2009-2016 microchip technology inc. 4.4 erasing flash program memory the minimum erase block is 32 words or 64 bytes. only through the use of an external programmer, or through icsp control, can larger blocks of program memory be bulk erased. word erase in the flash array is not supported. when initiating an erase sequence from the microcontroller itself, a block of 64 bytes of program memory is erased. the most significant 16 bits of the tblptr<21:6> point to the block being erased. the tblptr<5:0> bits are ignored. the eecon1 register commands the erase operation. the eepgd bit must be set to point to the flash program memory. the wren bit must be set to enable write operations. the free bit is set to select an erase operation. the write initiate sequence for eecon2, shown as steps 4 through 6 in section 4.4.1 ?flash program memory erase sequence? , is used to guard against accidental writes. this is sometimes referred to as a long write. a long write is necessary for erasing the internal flash. instruction execution is halted during the long write cycle. the long write is terminated by the internal programming timer. 4.4.1 flash program memory erase sequence the sequence of events for erasing a block of internal program memory is: 1. load table pointer register with address of block being erased. 2. set the eecon1 register for the erase operation: set eepgd bit to point to program memory; clear the cfgs bit to access program memory; set wren bit to enable writes; set free bit to enable the erase. 3. disable interrupts. 4. write 55h to eecon2. 5. write 0aah to eecon2. 6. set the wr bit. this will begin the block erase cycle. 7. the cpu will stall for duration of the erase (about 2 ms using internal timer). 8. re-enable interrupts. example 4-2: erasing a flash program memory block movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl erase_block bsf eecon1, eepgd ; point to flash program memory bcf eecon1, cfgs ; access flash program memory bsf eecon1, wren ; enable write to memory bsf eecon1, free ; enable block erase operation bcf intcon, gie ; disable interrupts required movlw 55h sequence movwf eecon2 ; write 55h movlw 0aah movwf eecon2 ; write 0aah bsf eecon1, wr ; start erase (cpu stall) bsf intcon, gie ; re-enable interrupts downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 51 pic18(l)f1xk22 4.5 writing to flash program memory the programming block size is 8 or 16 bytes, depending on the device (see ta b l e 4 - 1 ). word or byte programming is not supported. table writes are used internally to load the holding registers needed to program the flash memory. there are only as many holding registers as there are bytes in a write block (see table 4-1 ). since the table latch (tablat) is only a single byte, the tblwt instruction may need to be executed 8, or 16 times, depending on the device, for each programming operation. all of the table write operations will essentially be short writes because only the holding registers are written. after all the holding registers have been written, the programming operation of that block of memory is started by configuring the eecon1 register for a program memory write and performing the long write sequence. the long write is necessary for programming the internal flash. instruction execution is halted during a long write cycle. the long write will be terminated by the internal programming timer. the eeprom on-chip timer controls the write time. the write/erase voltages are generated by an on-chip charge pump, rated to operate over the voltage range of the device. figure 4-5: table writes to flash program memory 4.5.1 flash program memory write sequence the sequence of events for programming an internal program memory location should be: 1. read 64 bytes into ram. 2. update data values in ram as necessary. 3. load table pointer register with address being erased. 4. execute the block erase procedure. 5. load table pointer register with address of first byte being written. 6. write the 8 or 16 byte block into the holding registers with auto-increment. 7. set the eecon1 register for the write operation: set eepgd bit to point to program memory; clear the cfgs bit to access program memory; set wren to enable byte writes. 8. disable interrupts. 9. write 55h to eecon2. 10. write 0aah to eecon2. 11. set the wr bit. this will begin the write cycle. 12. the cpu will stall for duration of the write (about 2 ms using internal timer). 13. re-enable interrupts. 14. repeat steps 6 to 13 for each block until all 64 bytes are written. 15. verify the memory (table read). this procedure will require about 6 ms to update each write block of memory. an example of the required code is given in example 4-3 . note: the default value of the holding registers on device resets and after write operations is ffh. a write of ffh to a holding register does not modify that byte. this means that individual bytes of program memory may be modified, provided that the change does not attempt to change any bit from a 0 to a 1 . when modifying individual bytes, it is not necessary to load all holding registers before executing a long write operation. tablat tblptr = xxxxyy (1) tblptr = xxxx01 tblptr = xxxx00 write register tblptr = xxxx02 program memory holding register holding register holding register holding register 8 8 8 8 note 1: yy = x7 or xf for 8 or 16 byte write blocks, respectively. note: before setting the wr bit, the table pointer address needs to be within the intended address range of the bytes in the holding registers. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 52 ? 2009-2016 microchip technology inc. example 4-3: writing to flash program memory movlw d'64 ; number of bytes in erase block movwf counter movlw buffer_addr_high ; point to buffer movwf fsr0h movlw buffer_addr_low movwf fsr0l movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl read_block tblrd*+ ; read into tablat, and inc movf tablat, w ; get data movwf postinc0 ; store data decfsz counter ; done? bra read_block ; repeat modify_word movlw buffer_addr_high ; point to buffer movwf fsr0h movlw buffer_addr_low movwf fsr0l movlw new_data_low ; update buffer word movwf postinc0 movlw new_data_high movwf indf0 erase_block movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl bsf eecon1, eepgd ; point to flash program memory bcf eecon1, cfgs ; access flash program memory bsf eecon1, wren ; enable write to memory bsf eecon1, free ; enable erase operation bcf intcon, gie ; disable interrupts movlw 55h required movwf eecon2 ; write 55h sequence movlw 0aah movwf eecon2 ; write 0aah bsf eecon1, wr ; start erase (cpu stall) bsf intcon, gie ; re-enable interrupts tblrd*- ; dummy read decrement movlw buffer_addr_high ; point to buffer movwf fsr0h movlw buffer_addr_low movwf fsr0l write_buffer_back movlw blocksize ; number of bytes in holding register movwf counter movlw d64/blocksize ; number of write blocks in 64 bytes movwf counter2 write_byte_to_hregs movf postinc0, w ; get low byte of buffer data movwf tablat ; present data to table latch tblwt+* ; write data, perform a short write ; to internal tblwt holding register. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 53 pic18(l)f1xk22 example 4-3: writing to flash program memory (continued) 4.5.2 write verify depending on the application, good programming practice may dictate that the value written to the memory should be verified against the original value. this should be used in applications where excessive writes can stress bits near the specification limit. 4.5.3 unexpected termination of write operation if a write is terminated by an unplanned event, such as loss of power or an unexpected reset, the memory location just programmed should be verified and reprogrammed if needed. if the write operation is interrupted by a mclr reset or a wdt time-out reset during normal operation, the wrerr bit will be set which the user can check to decide whether a rewrite of the location(s) is needed. 4.5.4 protection against spurious writes to protect against spurious writes to flash program memory, the write initiate sequence must also be followed. see section 23.0 ?special features of the cpu? for more detail. 4.6 flash program operation during code protection see section 23.3 ?program verification and code protection? for details on code protection of flash program memory. table 4-3: registers associated with program flash memory decfsz counter ; loop until holding registers are full bra write_word_to_hregs program_memory bsf eecon1, eepgd ; point to flash program memory bcf eecon1, cfgs ; access flash program memory bsf eecon1, wren ; enable write to memory bcf intcon, gie ; disable interrupts movlw 55h required movwf eecon2 ; write 55h sequence movlw 0aah movwf eecon2 ; write 0aah bsf eecon1, wr ; start program (cpu stall) dcfsz counter2 ; repeat for remaining write blocks bra write_byte_to_hregs ; bsf intcon, gie ; re-enable interrupts bcf eecon1, wren ; disable write to memory name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page eecon1 eepgd cfgs free wrerr wren wr rd 247 eecon2 eeprom control register 2 (not a physical register) 247 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr2 oscfip c1ip c2ip eeip bclip tmr3ip 248 pie2 oscfie c1ie c2ie eeie bclie tmr3ie 248 pir2 oscfif c1if c2if eeif bclif tmr3if 248 tablat program memory table latch 245 tblptrl program memory table pointer low byte (tblptr<7:0>) 245 tblptru bit 21 program memory table pointer upper byte (tblptr<20:16>) 245 tbpltrh program memory table pointer high byte (tblptr<15:8>) 245 legend: = unimplemented, read as 0 . shaded cells are not used during flash/eeprom access. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 54 ? 2009-2016 microchip technology inc. 5.0 data eeprom memory the data eeprom is a nonvolatile memory array, separate from the data ram and program memory, which is used for long-term storage of program data. it is not directly mapped in either the register file or program memory space but is indirectly addressed through the special function registers (sfrs). the eeprom is readable and writable during normal operation over the entire v dd range. four sfrs are used to read and write to the data eeprom as well as the program memory. they are: eecon1 eecon2 eedata eeadr the data eeprom allows byte read and write. when interfacing to the data memory block, eedata holds the 8-bit data for read/write and the eeadr register holds the address of the eeprom location being accessed. the eeprom data memory is rated for high erase/write cycle endurance. a byte write automatically erases the location and writes the new data (erase-before-write). the write time is controlled by an on-chip timer; it will vary with voltage and temperature as well as from chip- to-chip. please refer to parameter us122 ( table 26-24 ) for exact limits. 5.1 eeadr register the eeadr register is used to address the data eeprom for read and write operations. the 8-bit range of the register can address a memory range of 256 bytes (00h to ffh). 5.2 eecon1 and eecon2 registers access to the data eeprom is controlled by two registers: eecon1 and eecon2. these are the same registers which control access to the program memory and are used in a similar manner for the data eeprom. the eecon1 register ( register 5-1 ) is the control register for data and program memory access. control bit eepgd determines if the access will be to program or data eeprom memory. when the eepgd bit is clear, operations will access the data eeprom memory. when the eepgd bit is set, program memory is accessed. control bit, cfgs, determines if the access will be to the configuration registers or to program memory/data eeprom memory. when the cfgs bit is set, subsequent operations access configuration registers. when the cfgs bit is clear, the eepgd bit selects either program flash or data eeprom memory. the wren bit, when set, will allow a write operation. on power-up, the wren bit is clear. the wrerr bit is set by hardware when the wr bit is set and cleared when the internal programming timer expires and the write operation is complete. the wr control bit initiates write operations. the bit can be set but not cleared by software. it is cleared only by hardware at the completion of the write operation. control bits, rd and wr, start read and erase/write operations, respectively. these bits are set by firmware and cleared by hardware at the completion of the operation. the rd bit cannot be set when accessing program memory (eepgd = 1 ). program memory is read using table read instructions. see section 4.1 ?table reads and table writes? regarding table reads. the eecon2 register is not a physical register. it is used exclusively in the memory write and erase sequences. reading eecon2 will read all 0 s. note: during normal operation, the wrerr may read as 1 . this can indicate that a write operation was prematurely terminated by a reset, or a write operation was attempted improperly. note: the eeif interrupt flag bit of the pir2 register is set when the write is complete. it must be cleared by software. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 55 pic18(l)f1xk22 register 5-1: eecon1: data eeprom control 1 register r/w-x r/w-x u-0 r/w-0 r/w-x r/w-0 r/s-0 r/s-0 eepgd cfgs free wrerr wren wr rd bit 7 bit 0 legend: r = readable bit w = writable bit s = bit can be set by software, but not cleared u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 eepgd: flash program or data eeprom memory select bit 1 = access flash program memory 0 = access data eeprom memory bit 6 cfgs: flash program/data eeprom or configuration select bit 1 = access configuration registers 0 = access flash program or data eeprom memory bit 5 unimplemented: read as 0 bit 4 free: flash row (block) erase enable bit 1 = erase the program memory block addressed by tblptr on the next wr command (cleared by completion of erase operation) 0 = perform write-only bit 3 wrerr: flash program/data eeprom error flag bit (1) 1 = a write operation is prematurely terminated (any reset during self-timed programming in normal operation, or an improper write attempt) 0 = the write operation completed bit 2 wren: flash program/data eeprom write enable bit 1 = allows write cycles to flash program/data eeprom 0 = inhibits write cycles to flash program/data eeprom bit 1 wr: write control bit 1 = initiates a data eeprom erase/write cycle or a program memory erase cycle or write cycle. (the operation is self-timed and the bit is cleared by hardware once write is complete. the wr bit can only be set (not cleared) by software.) 0 = write cycle to the eeprom is complete bit 0 rd: read control bit 1 = initiates an eeprom read (read takes one cycle. rd is cleared by hardware. the rd bit can only be set (not cleared) by software. rd bit cannot be set when eepgd = 1 or cfgs = 1 .) 0 = does not initiate an eeprom read note 1: when a wrerr occurs, the eepgd and cfgs bits are not cleared. this allows tracing of the error condition. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 56 ? 2009-2016 microchip technology inc. 5.3 reading the data eeprom memory to read a data memory location, the user must write the address to the eeadr register, clear the eepgd control bit of the eecon1 register and then set control bit, rd. the data is available on the very next instruction cycle; therefore, the eedata register can be read by the next instruction. eedata will hold this value until another read operation, or until it is written to by the user (during a write operation). the basic process is shown in example 5-1 . 5.4 writing to the data eeprom memory to write an eeprom data location, the address must first be written to the eeadr register and the data written to the eedata register. the sequence in example 5-2 must be followed to initiate the write cycle. the write will not begin if this sequence is not exactly followed (write 55h to eecon2, write 0aah to eecon2, then set wr bit) for each byte. it is strongly recommended that interrupts be disabled during this code segment. additionally, the wren bit in eecon1 must be set to enable writes. this mechanism prevents accidental writes to data eeprom due to unexpected code execution (i.e., runaway programs). the wren bit should be kept clear at all times, except when updating the eeprom. the wren bit is not cleared by hardware. after a write sequence has been initiated, eecon1, eeadr and eedata cannot be modified. the wr bit will be inhibited from being set unless the wren bit is set. both wr and wren cannot be set with the same instruction. at the completion of the write cycle, the wr bit is cleared by hardware and the eeprom interrupt flag bit, eeif, is set. the user may either enable this interrupt or poll this bit. eeif must be cleared by software. 5.5 write verify depending on the application, good programming practice may dictate that the value written to the memory should be verified against the original value. this should be used in applications where excessive writes can stress bits near the specification limit. example 5-1: data eeprom read example 5-2: data eeprom write movlw data_ee_addr ; movwf eeadr ; data memory address to read bcf eecon1, eepgd ; point to data memory bcf eecon1, cfgs ; access eeprom bsf eecon1, rd ; eeprom read movf eedata, w ; w = eedata movlw data_ee_addr_low ; movwf eeadr ; data memory address to write movlw data_ee_data ; movwf eedata ; data memory value to write bcf eecon1, eepgd ; point to data memory bcf eecon1, cfgs ; access eeprom bsf eecon1, wren ; enable writes bcf intcon, gie ; disable interrupts movlw 55h ; required movwf eecon2 ; write 55h sequence movlw 0aah ; movwf eecon2 ; write 0aah bsf eecon1, wr ; set wr bit to begin write bsf intcon, gie ; enable interrupts ; user code execution bcf eecon1, wren ; disable writes on write complete (eeif set) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 57 pic18(l)f1xk22 5.6 operation during code-protect data eeprom memory has its own code-protect bits in configuration words. external read and write operations are disabled if code protection is enabled. the microcontroller itself can both read and write to the internal data eeprom, regardless of the state of the code-protect configuration bit. refer to section 23.0 ?special features of the cpu? for additional information. 5.7 protection against spurious write there are conditions when the user may not want to write to the data eeprom memory. to protect against spurious eeprom writes, various mechanisms have been implemented. on power-up, the wren bit is cleared. in addition, writes to the eeprom are blocked during the power-up timer period (t pwrt , parameter 33). the write initiate sequence and the wren bit together help prevent an accidental write during brown-out, power glitch or software malfunction. 5.8 using the data eeprom the data eeprom is a high-endurance, byte addressable array that has been optimized for the storage of frequently changing information (e.g., program variables or other data that are updated often). when variables in one section change frequently, while variables in another section do not change, it is possible to exceed the total number of write cycles to the eeprom without exceeding the total number of write cycles to a single byte. if this is the case, then an array refresh must be performed. for this reason, variables that change infrequently (such as constants, ids, calibration, etc.) should be stored in flash program memory. a simple data eeprom refresh routine is shown in example 5-3 . example 5-3: data eeprom refresh routine table 5-1: registers associated with data eeprom memory note: if data eeprom is only used to store constants and/or data that changes rarely, an array refresh is likely not required. see specification. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page eeadr eeadr7 eeadr6 eeadr5 eeadr4 eeadr3 eeadr2 eeadr1 eeadr0 247 eecon1 eepgd cfgs free wrerr wren wr rd 247 eecon2 eeprom control register 2 (not a physical register) 247 eedata eeprom data register 247 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr2 oscfip c1ip c2ip eeip bclip tmr3ip 248 pie2 oscfie c1ie c2ie eeie bclie tmr3ie 248 pir2 oscfif c1if c2if eeif bclif tmr3if 248 legend: = unimplemented, read as 0 . shaded cells are not used during flash/eeprom access. clrf eeadr ; start at address 0 bcf eecon1, cfgs ; set for memory bcf eecon1, eepgd ; set for data eeprom bcf intcon, gie ; disable interrupts bsf eecon1, wren ; enable writes loop ; loop to refresh array bsf eecon1, rd ; read current address movlw 55h ; movwf eecon2 ; write 55h movlw 0aah ; movwf eecon2 ; write 0aah bsf eecon1, wr ; set wr bit to begin write btfsc eecon1, wr ; wait for write to complete bra $-2 incfsz eeadr, f ; increment address bra loop ; not zero, do it again bcf eecon1, wren ; disable writes bsf intcon, gie ; enable interrupts downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 58 ? 2009-2016 microchip technology inc. 6.0 8 x 8 hardware multiplier 6.1 introduction all pic18 devices include an 8 x 8 hardware multiplier as part of the alu. the multiplier performs an unsigned operation and yields a 16-bit result that is stored in the product register pair, prodh:prodl. the multipliers operation does not affect any flags in the status register. making multiplication a hardware operation allows it to be completed in a single instruction cycle. this has the advantages of higher computational throughput and reduced code size for multiplication algorithms and allows the pic18 devices to be used in many applications previously reserved for digital signal processors. a comparison of various hardware and software multiply operations, along with the savings in memory and execution time, is shown in ta b l e 6 - 1 . 6.2 operation example 6-1 shows the instruction sequence for an 8 x 8 unsigned multiplication. only one instruction is required when one of the arguments is already loaded in the wreg register. example 6-2 shows the sequence to do an 8 x 8 signed multiplication. to account for the sign bits of the arguments, each arguments most significant bit (msb) is tested and the appropriate subtractions are done. example 6-1: 8 x 8 unsigned multiply routine example 6-2: 8 x 8 signed multiply routine table 6-1: performance comparison for v arious multiply operations movf arg1, w ; mulwf arg2 ; arg1 * arg2 -> ; prodh:prodl movf arg1, w mulwf arg2 ; arg1 * arg2 -> ; prodh:prodl btfsc arg2, sb ; test sign bit subwf prodh, f ; prodh = prodh ; - arg1 movf arg2, w btfsc arg1, sb ; test sign bit subwf prodh, f ; prodh = prodh ; - arg2 routine multiply method program memory (words) cycles (max) time @ 40 mhz @ 10 mhz @ 4 mhz 8 x 8 unsigned without hardware multiply 13 69 6.9 ? s2 7 . 6 ? s6 9 ? s hardware multiply 1 1 100 ns 400 ns 1 ? s 8 x 8 signed without hardware multiply 33 91 9.1 ? s3 6 . 4 ? s9 1 ? s hardware multiply 6 6 600 ns 2.4 ? s6 ? s 16 x 16 unsigned without hardware multiply 21 242 24.2 ? s9 6 . 8 ? s 242 ? s hardware multiply 28 28 2.8 ? s 11.2 ? s2 8 ? s 16 x 16 signed without hardware multiply 52 254 25.4 ? s 102.6 ? s 254 ? s hardware multiply 35 40 4.0 ? s1 6 . 0 ? s4 0 ? s downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 59 pic18(l)f1xk22 example 6-3 shows the sequence to do a 16 x 16 unsigned multiplication. equation 6-1 shows the algorithm that is used. the 32-bit result is stored in four registers (res<3:0>). equation 6-1: 16 x 16 unsigned multiplication algorithm example 6-3: 16 x 16 unsigned multiply routine example 6-4 shows the sequence to do a 16 x 16 signed multiply. equation 6-2 shows the algorithm used. the 32-bit result is stored in four registers (res<3:0>). to account for the sign bits of the arguments, the msb for each argument pair is tested and the appropriate subtractions are done. equation 6-2: 16 x 16 signed multiplication algorithm example 6-4: 16 x 16 signed multiply routine res3:res0 = arg1h:arg1l ? arg2h:arg2l = (arg1h ? arg2h ? 2 16 ) + (arg1h ? arg2l ? 2 8 ) + (arg1l ? arg2h ? 2 8 ) + (arg1l ? arg2l) movf arg1l, w mulwf arg2l ; arg1l * arg2l-> ; prodh:prodl movff prodh, res1 ; movff prodl, res0 ; ; movf arg1h, w mulwf arg2h ; arg1h * arg2h-> ; prodh:prodl movff prodh, res3 ; movff prodl, res2 ; ; movf arg1l, w mulwf arg2h ; arg1l * arg2h-> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; ; movf arg1h, w ; mulwf arg2l ; arg1h * arg2l-> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; res3:res0 = arg1h:arg1l ? arg2h:arg2l = (arg1h ? arg2h ? 2 16 ) + (arg1h ? arg2l ? 2 8 ) + (arg1l ? arg2h ? 2 8 ) + (arg1l ? arg2l) + (-1 ? arg2h<7> ? arg1h:arg1l ? 2 16 ) + (-1 ? arg1h<7> ? arg2h:arg2l ? 2 16 ) movf arg1l, w mulwf arg2l ; arg1l * arg2l -> ; prodh:prodl movff prodh, res1 ; movff prodl, res0 ; ; movf arg1h, w mulwf arg2h ; arg1h * arg2h -> ; prodh:prodl movff prodh, res3 ; movff prodl, res2 ; ; movf arg1l, w mulwf arg2h ; arg1l * arg2h -> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; ; movf arg1h, w ; mulwf arg2l ; arg1h * arg2l -> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; ; btfss arg2h, 7 ; arg2h:arg2l neg? bra sign_arg1 ; no, check arg1 movf arg1l, w ; subwf res2 ; movf arg1h, w ; subwfb res3 ; sign_arg1 btfss arg1h, 7 ; arg1h:arg1l neg? bra cont_code ; no, done movf arg2l, w ; subwf res2 ; movf arg2h, w ; subwfb res3 ; cont_code : downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 60 ? 2009-2016 microchip technology inc. 7.0 interrupts the pic18(l)f1xk22 devices have multiple interrupt sources and an interrupt priority feature that allows most interrupt sources to be assigned a high priority level or a low priority level. the high priority interrupt vector is at 0008h and the low priority interrupt vector is at 0018h. a high priority interrupt event will interrupt a low priority interrupt that may be in progress. there are twelve registers which are used to control interrupt operation. these registers are: rcon intcon intcon2 intcon3 pir1, pir2 pie1, pie2 ipr1, ipr2 it is recommended that the microchip header files supplied with mplab ? ide be used for the symbolic bit names in these registers. this allows the assembler/ compiler to automatically take care of the placement of these bits within the specified register. in general, interrupt sources have three bits to control their operation. they are: flag bit to indicate that an interrupt event occurred enable bit that allows program execution to branch to the interrupt vector address when the flag bit is set priority bit to select high priority or low priority 7.1 mid-range compatibility when the ipen bit is cleared (default state), the interrupt priority feature is disabled and interrupts are compatible with pic ? microcontroller mid-range devices. in compatibility mode, the interrupt priority bits of the iprx registers have no effect. the peie bit of the intcon register is the global interrupt enable for the peripherals. the peie bit disables only the peripheral interrupt sources and enables the peripheral interrupt sources when the gie bit is also set. the gie bit of the intcon register is the global interrupt enable which enables all non-peripheral interrupt sources and disables all interrupt sources, including the peripherals. all interrupts branch to address 0008h in compatibility mode. 7.2 interrupt priority the interrupt priority feature is enabled by setting the ipen bit of the rcon register. when interrupt priority is enabled the gie and peie global interrupt enable bits of compatibility mode are replaced by the gieh high priority, and giel low priority, global interrupt enables. when set, the gieh bit of the intcon register enables all interrupts that have their associated iprx register or intconx register priority bit set (high priority). when clear, the giel bit disables all interrupt sources including those selected as low priority. when clear, the giel bit of the intcon register disables only the interrupts that have their associated priority bit cleared (low priority). when set, the giel bit enables the low priority sources when the gieh bit is also set. when the interrupt flag, enable bit and appropriate global interrupt enable bit are all set, the interrupt will vector immediately to address 0008h for high priority, or 0018h for low priority, depending on level of the interrupting sources priority bit. individual interrupts can be disabled through their corresponding interrupt enable bits. 7.3 interrupt response when an interrupt is responded to, the global interrupt enable bit is cleared to disable further interrupts. the gie bit is the global interrupt enable when the ipen bit is cleared. when the ipen bit is set, enabling interrupt priority levels, the gieh bit is the high priority global interrupt enable and the giel bit is the low priority global interrupt enable. high-priority interrupt sources can interrupt a low-priority interrupt. low-priority interrupts are not processed while high-priority interrupts are in progress. the return address is pushed onto the stack and the pc is loaded with the interrupt vector address (0008h or 0018h). once in the interrupt service routine, the source(s) of the interrupt can be determined by polling the interrupt flag bits in the intconx and pirx registers. the interrupt flag bits must be cleared by software before re-enabling interrupts to avoid repeating the same interrupt. the return-from-interrupt instruction, retfie , exits the interrupt routine and sets the gie bit (gieh or giel if priority levels are used), which re-enables interrupts. for external interrupt events, such as the int pins or the portb interrupt-on-change, the interrupt latency will be three to four instruction cycles. the exact latency is the same for one-cycle or two-cycle instructions. individual interrupt flag bits are set, regardless of the status of their corresponding enable bits or the global interrupt enable bit. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 61 pic18(l)f1xk22 figure 7-1: pic18 interrupt logic note: do not use the movff instruction to modify any of the interrupt control registers while any interrupt is enabled. doing so may cause erratic microcontroller behavior. tmr0ie gieh/gie giel/peie wake-up if in interrupt to cpu vector to location 0008h int2if int2ie int2ip int1if int1ie int1ip tmr0if tmr0ie tmr0ip rabif rabie rabip ipen tmr0if tmr0ip int1if int1ie int1ip int2if int2ie int2ip rabif rabie rabip int0if int0ie giel/peie interrupt to cpu vector to location ipen ipen 0018h sspif sspie sspip sspif sspie sspip adif adie adip rcif rcie rcip additional peripheral interrupts adif adie adip high priority interrupt generation low priority interrupt generation rcifrcie rcip additional peripheral interrupts idle or sleep modes gieh/gie note 1: the rabif interrupt also requires the individual pin ioca and iocb enable. (1) (1) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 62 ? 2009-2016 microchip technology inc. 7.4 intcon registers the intcon registers are readable and writable registers, which contain various enable, priority and flag bits. note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling. register 7-1: intcon: interrupt control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-x gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 gie/gieh: global interrupt enable bit when ipen = 0 : 1 = enables all unmasked interrupts 0 = disables all interrupts including peripherals when ipen = 1 : 1 = enables all high priority interrupts 0 = disables all interrupts including low priority bit 6 peie/giel: peripheral interrupt enable bit when ipen = 0 : 1 = enables all unmasked peripheral interrupts 0 = disables all peripheral interrupts when ipen = 1 : 1 = enables all low priority interrupts 0 = disables all low priority interrupts bit 5 tmr0ie: tmr0 overflow interrupt enable bit 1 = enables the tmr0 overflow interrupt 0 = disables the tmr0 overflow interrupt bit 4 int0ie: int0 external interrupt enable bit 1 = enables the int0 external interrupt 0 = disables the int0 external interrupt bit 3 rabie: ra and rb port change interrupt enable bit (2) 1 = enables the ra and rb port change interrupt 0 = disables the ra and rb port change interrupt bit 2 tmr0if: tmr0 overflow interrupt flag bit 1 = tmr0 register has overflowed (must be cleared by software) 0 = tmr0 register did not overflow bit 1 int0if: int0 external interrupt flag bit 1 = the int0 external interrupt occurred (must be cleared by software) 0 = the int0 external interrupt did not occur bit 0 rabif: ra and rb port change interrupt flag bit (1) 1 = at least one of the ra <5:0> or rb<7:4> pins changed state (must be cleared by software) 0 = none of the ra<5:0> or rb<7:4> pins have changed state note 1: a mismatch condition will continue to set the rabif bit. reading porta and portb will end the mismat ch condition and allow the bit to be cleared. 2: ra and rb port change interrupts also require the individual pin ioca and iocb enable. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 63 pic18(l)f1xk22 register 7-2: intcon2: interrupt control 2 register r/w-1 r/w-1 r/w-1 r/w-1 u-0 r/w-1 u-0 r/w-1 rabpu intedg0 intedg1 intedg2 t m r 0 i p rabip bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 rabpu : porta and portb pull-up enable bit 1 = porta and portb pull-ups are disabled 0 = porta and portb pull-ups are enabled provided that the pin is an input and the corresponding wpua and wpub bits are set. bit 6 intedg0: external interrupt 0 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 5 intedg1: external interrupt 1 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 4 intedg2: external interrupt 2 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 3 unimplemented: read as 0 bit 2 tmr0ip: tmr0 overflow interrupt priority bit 1 =high priority 0 = low priority bit 1 unimplemented: read as 0 bit 0 rabip: ra and rb port change interrupt priority bit 1 =high priority 0 = low priority note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software might ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 64 ? 2009-2016 microchip technology inc. register 7-3: intcon3: interrupt control 3 register r/w-1 r/w-1 u-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 int2ip int1ip int2ie int1ie int2if int1if bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 int2ip: int2 external interrupt priority bit 1 = high priority 0 = low priority bit 6 int1ip: int1 external interrupt priority bit 1 = high priority 0 = low priority bit 5 unimplemented: read as 0 bit 4 int2ie: int2 external interrupt enable bit 1 = enables the int2 external interrupt 0 = disables the int2 external interrupt bit 3 int1ie: int1 external interrupt enable bit 1 = enables the int1 external interrupt 0 = disables the int1 external interrupt bit 2 unimplemented: read as 0 bit 1 int2if: int2 external interrupt flag bit 1 = the int2 external interrupt occurred (must be cleared by software) 0 = the int2 external interrupt did not occur bit 0 int1if: int1 external interrupt flag bit 1 = the int1 external interrupt occurred (must be cleared by software) 0 = the int1 external interrupt did not occur note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software might ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 65 pic18(l)f1xk22 7.5 pir registers the pir registers contain the individual flag bits for the peripheral interrupts. due to the number of peripheral interrupt sources, there are two peripheral interrupt request flag registers (pir1 and pir2). note 1: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global interrupt enable bit, gie of the intcon register. 2: user software might ensure the appropriate interrupt flag bits are cleared prior to enabling an interrupt and after servicing that interrupt. register 7-4: pir1: peripheral interrupt request (flag) register 1 u-0 r/w-0 r-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 adif rcif txif sspif ccp1if tmr2if tmr1if bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 unimplemented: read as 0 bit 6 adif: a/d converter interrupt flag bit 1 = an a/d conversion completed (must be cleared by software) 0 = the a/d conversion is not complete or has not been started bit 5 rcif: eusart receive interrupt flag bit 1 = the eusart receive buffer, rcreg, is full (cleared when rcreg is read) 0 = the eusart receive buffer is empty bit 4 txif: eusart transmit interrupt flag bit 1 = the eusart transmit buffer, txreg, is empty (cleared when txreg is w ritten) 0 = the eusart transmit buffer is full bit 3 sspif: master synchronous serial port interrupt flag bit 1 = the transmission/reception is complete (must be cleared by software) 0 = waiting to transmit/receive bit 2 ccp1if: ccp1 interrupt flag bit capture mode: 1 = a tmr1 register capture occurred (must be cleared by software) 0 = no tmr1 register capture occurred compare mode: 1 = a tmr1 register compare match occurred (must be cleared by software) 0 = no tmr1 register compare match occurred pwm mode: unused in this mode bit 1 tmr2if: tmr2 to pr2 match interrupt flag bit 1 = tmr2 to pr2 match occurred (must be cleared by software) 0 = no tmr2 to pr2 match occurred bit 0 tmr1if: tmr1 overflow interrupt flag bit 1 = tmr1 register overflowed (must be cleared by software) 0 = tmr1 register did not overflow note 1: the pspif bit is unimplemented on 28-pin devices and will read as 0 . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 66 ? 2009-2016 microchip technology inc. register 7-5: pir2: peripheral interrupt request (flag) register 2 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 u-0 r/w-0 u-0 oscfif c1if c2if eeif bclif t m r 3 i f bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 oscfif: oscillator fail interrupt flag bit 1 = device oscillator failed, clock input has changed to hfintosc (must be cleared by software) 0 = device clock operating bit 6 c1if: comparator c1 interrupt flag bit 1 = comparator c1 output has changed (must be cleared by software) 0 = comparator c1 output has not changed bit 5 c2if: comparator c2 interrupt flag bit 1 = comparator c2 output has changed (must be cleared by software) 0 = comparator c2 output has not changed bit 4 eeif: data eeprom/flash write operation interrupt flag bit 1 = the write operation is complete (must be cleared by software) 0 = the write operation is not complete or has not been started bit 3 bclif: bus collision interrupt flag bit 1 = a bus collision occurred (must be cleared by software) 0 = no bus collision occurred bit 2 unimplemented: read as 0 bit 1 tmr3if: tmr3 overflow interrupt flag bit 1 = tmr3 register overflowed (must be cleared by software) 0 = tmr3 register did not overflow bit 0 unimplemented: read as 0 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 67 pic18(l)f1xk22 7.6 pie registers the pie registers contain the individual enable bits for the peripheral interrupts. due to the number of peripheral interrupt sources, there are two peripheral interrupt enable registers (pie1 and pie2). when ipen = 0 , the peie bit must be set to enable any of these peripheral interrupts. register 7-6: pie1: peripheral interrupt enable (flag) register 1 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 adie rcie txie sspie ccp1ie tmr2ie tmr1ie bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 unimplemented: read as 0 bit 6 adie: a/d converter interrupt enable bit 1 = enables the a/d interrupt 0 = disables the a/d interrupt bit 5 rcie: eusart receive interrupt enable bit 1 = enables the eusart receive interrupt 0 = disables the eusart receive interrupt bit 4 txie: eusart transmit interrupt enable bit 1 = enables the eusart transmit interrupt 0 = disables the eusart transmit interrupt bit 3 sspie: master synchronous serial port interrupt enable bit 1 = enables the mssp interrupt 0 = disables the mssp interrupt bit 2 ccp1ie: ccp1 interrupt enable bit 1 = enables the ccp1 interrupt 0 = disables the ccp1 interrupt bit 1 tmr2ie: tmr2 to pr2 match interrupt enable bit 1 = enables the tmr2 to pr2 match interrupt 0 = disables the tmr2 to pr2 match interrupt bit 0 tmr1ie: tmr1 overflow interrupt enable bit 1 = enables the tmr1 overflow interrupt 0 = disables the tmr1 overflow interrupt downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 68 ? 2009-2016 microchip technology inc. register 7-7: pie2: peripheral interrupt enable (flag) register 2 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 u-0 r/w-0 u-0 oscfie c1ie c2ie eeie bclie t m r 3 i e bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 oscfie: oscillator fail interrupt enable bit 1 = enabled 0 =disabled bit 6 c1ie: comparator c1 interrupt enable bit 1 = enabled 0 = disabled bit 5 c2ie: comparator c2 interrupt enable bit 1 = enabled 0 = disabled bit 4 eeie: data eeprom/flash write operation interrupt enable bit 1 = enabled 0 =disabled bit 3 bclie: bus collision interrupt enable bit 1 = enabled 0 =disabled bit 2 unimplemented: read as 0 bit 1 tmr3ie: tmr3 overflow interrupt enable bit 1 = enabled 0 =disabled bit 0 unimplemented: read as 0 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 69 pic18(l)f1xk22 7.7 ipr registers the ipr registers contain the individual priority bits for the peripheral interrupts. due to the number of peripheral interrupt sources, there are two peripheral interrupt priority registers (ipr1 and ipr2). using the priority bits requires that the interrupt priority enable (ipen) bit be set. register 7-8: ipr1: peripheral interrupt priority register 1 u-0 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 adip rcip txip sspip ccp1ip tmr2ip tmr1ip bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 unimplemented: read as 0 bit 6 adip: a/d converter interrupt priority bit 1 = high priority 0 = low priority bit 5 rcip: eusart receive interrupt priority bit 1 = high priority 0 = low priority bit 4 txip: eusart transmit interrupt priority bit 1 = high priority 0 = low priority bit 3 sspip: master synchronous serial port interrupt priority bit 1 = high priority 0 = low priority bit 2 ccp1ip: ccp1 interrupt priority bit 1 = high priority 0 = low priority bit 1 tmr2ip: tmr2 to pr2 match interrupt priority bit 1 = high priority 0 = low priority bit 0 tmr1ip: tmr1 overflow interrupt priority bit 1 = high priority 0 = low priority downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 70 ? 2009-2016 microchip technology inc. register 7-9: ipr2: peripheral interrupt priority register 2 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 u-0 r/w-1 u-0 oscfip c1ip c2ip eeip bclip t m r 3 i p bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 oscfip: oscillator fail interrupt priority bit 1 = high priority 0 = low priority bit 6 c1ip: comparator c1 interrupt priority bit 1 = high priority 0 = low priority bit 5 c2ip: comparator c2 interrupt priority bit 1 = high priority 0 = low priority bit 4 eeip: data eeprom/flash write operation interrupt priority bit 1 = high priority 0 = low priority bit 3 bclip: bus collision interrupt priority bit 1 = high priority 0 = low priority bit 2 unimplemented: read as 0 bit 1 tmr3ip: tmr3 overflow interrupt priority bit 1 = high priority 0 = low priority bit 0 unimplemented: read as 0 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 71 pic18(l)f1xk22 7.8 rcon register the rcon register contains flag bits which are used to determine the cause of the last reset or wake-up from idle or sleep modes. rcon also contains the ipen bit which enables interrupt priorities. the operation of the sboren bit and the reset flag bits is discussed in more detail in section 22.1 ?rcon register? . register 7-10: rcon: re set control register r/w-0 r/w-1 u-0 r/w-1 r-1 r-1 r/w-0 r/w-0 ipen sboren (1) r i to pd por (2) bor (3) bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 ipen: interrupt priority enable bit 1 = enable priority levels on interrupts 0 = disable priority levels on interrupts (pic16cxxx compatibility mode) bit 6 sboren: bor software enable bit (1) if boren<1:0> = 01 : 1 = bor is enabled 0 = bor is disabled if boren<1:0> = 00 , 10 or 11 : bit is disabled and read as 0 . bit 5 unimplemented: read as 0 bit 4 ri : reset instruction flag bit 1 = the reset instruction was not executed (set by firmware or power-on reset) 0 = the reset instruction was executed causing a device reset (must be set in firmware after a code-executed reset occurs) bit 3 to : watchdog time-out flag bit 1 = set by power-up, clrwdt instruction or sleep instruction 0 = a wdt time-out occurred bit 2 pd : power-down detection flag bit 1 = set by power-up or by the clrwdt instruction 0 = set by execution of the sleep instruction bit 1 por : power-on reset status bit (2) 1 = no power-on reset occurred 0 = a power-on reset occurred (must be set in software after a power-on reset occurs) bit 0 bor : brown-out reset status bit (3) 1 = a brown-out reset has not occurred (set by firmware only) 0 = a brown-out reset occurred (must be set by firmware after a por or brown-out reset occurs) note 1: if sboren is enabled, its reset state is 1 ; otherwise, it is 0 . 2: the actual reset value of por is determined by the type of device reset. see the notes following this register and section 22.6 ?reset state of registers? for additional information. 3: see table 22-3 . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 72 ? 2009-2016 microchip technology inc. 7.9 intx pin interrupts external interrupts on the int0, int1 and int2 pins are edge-triggered. if the corresponding intedgx bit in the intcon2 register is set (= 1 ), the interrupt is triggered by a rising edge; if the bit is clear, the trigger is on the falling edge. when a valid edge appears on the intx pin, the corresponding flag bit, intxf, is set. this interrupt can be disabled by clearing the corresponding enable bit, intxe. flag bit, intxf, must be cleared by software in the interrupt service routine before re- enabling the interrupt. all external interrupts (int0, int1 and int2) can wake- up the processor from idle or sleep modes if bit intxe was set prior to going into those modes. if the global interrupt enable bit, gie, is set, the processor will branch to the interrupt vector following wake-up. interrupt priority for int1 and int2 is determined by the value contained in the interrupt priority bits, int1ip and int2ip of the intcon3 register. there is no priority bit associated with int0. it is always a high- priority interrupt source. 7.10 tmr0 interrupt in 8-bit mode (which is the default), an overflow in the tmr0 register (ffh ? 00h) will set flag bit, tmr0if. in 16-bit mode, an overflow in the tmr0h:tmr0l register pair (ffffh ?? 0000h) will set tmr0if. the interrupt can be enabled/disabled by setting/clearing enable bit, tmr0ie of the intcon register. interrupt priority for timer0 is determined by the value contained in the interrupt priority bit, tmr0ip of the intcon2 register. see section 9.0 ?timer0 module? for further details on the timer0 module. 7.11 porta and portb interrupt-on- change an input change on porta or portb sets flag bit, rabif of the intcon register. the interrupt can be enabled/disabled by setting/clearing enable bit, rabie of the intcon register. pins must also be individually enabled with the ioca and iocb register. interrupt priority for porta and portb interrupt-on-change is determined by the value contained in the interrupt priority bit, rabip of the intcon2 register. 7.12 context saving during interrupts during interrupts, the return pc address is saved on the stack. additionally, the wreg, status and bsr registers are saved on the fast return stack. if a fast return from interrupt is not used (see section 3.3 ?data memory organization? ), the user may need to save the wreg, status and bsr registers on entry to the interrupt service routine. depending on the users application, other registers may also need to be saved. example 7-1 saves and restores the wreg, status and bsr registers during an interrupt service routine. example 7-1: saving status, wr eg and bsr registers in ram movwf w_temp ; w_temp is in virtual bank movff status, status_temp ; status_temp located anywhere movff bsr, bsr_temp ; bsr_tmep located anywhere ; ; user isr code ; movff bsr_temp, bsr ; restore bsr movf w_temp, w ; restore wreg movff status_temp, status ; restore status downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 73 pic18(l)f1xk22 8.0 i/o ports there are up to three ports available. some pins of the i/o ports are multiplexed with an alternate function from the peripheral features on the device. in general, when a peripheral is enabled, that pin may not be used as a general purpose i/o pin. each port has three registers for its operation. these registers are: tris register (data direction register) port register (reads the levels on the pins of the device) lat register (output latch) the porta data latch (lata register) is useful for read-modify-write operations on the value that the i/o pins are driving. a simplified model of a generic i/o port, without the interfaces to other peripherals, is shown in figure 8-1 . figure 8-1: generic i/o port operation 8.1 porta, trisa and lata registers porta is a 6-bit wide, bidirectional port, with the exception of ra3, which is input-only and its tris bit will always read as 1 . the corresponding data direction register is trisa. setting a trisa bit (= 1 ) will make the corresponding porta pin an input (i.e., disable the output driver). clearing a trisa bit (= 0 ) will make the corresponding porta pin an output (i.e., enable the output driver and put the contents of the output latch on the selected pin). reading the porta register reads the status of the pins, whereas writing to it, will write to the port latch. the porta data latch (lata) register is also memory mapped. read-modify-write operations on the lata register read and write the latched output value for porta. all of the porta pins are individually configurable as interrupt-on-change pins. control bits in the ioca register enable (when set) or disable (when clear) the interrupt function for each pin. when set, the rabie bit of the intcon register enables interrupts on all pins which also have their corresponding ioca bit set. when clear, the rabie bit disables all interrupt-on-changes. only pins configured as inputs can cause this interrupt to occur (i.e., any pin configured as an output is excluded from the interrupt-on-change comparison). for enabled interrupt-on-change pins, the values are compared with the old value latched on the last read of porta. the mismatch outputs of the last read are ord together to set the porta change interrupt flag bit (rabif) in the intcon register. data bus wr lat wr tris rd port data latch tris latch rd tris input buffer i/o pin (1) q d ck q d ck en qd en rd lat or port note 1: i/o pins have diode protection to v dd and v ss . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 74 ? 2009-2016 microchip technology inc. this interrupt can wake the device from the sleep mode, or any of the idle modes. the user, in the interrupt service routine, can clear the interrupt in the following manner: a) any read or write of porta to clear the mismatch condition (except when porta is the source or destination of a movff instruction). b) clear the flag bit, rabif. a mismatch condition will continue to set the rabif flag bit. reading or writing porta will end the mismatch condition and allow the rabif bit to be cleared. the latch holding the last read value is not affected by a mclr nor brown-out reset. after either one of these resets, the rabif flag will continue to be set if a mismatch is present. the interrupt-on-change feature is recommended for wake-up on key depression operation and operations where porta is only used for the interrupt-on-change feature. polling of porta is not recommended while using the interrupt-on-change feature. each of the porta pins has an individually controlled weak internal pull-up. when set, each bit of the wpua register enables the corresponding pin pull-up. when cleared, the rabpu bit of the intcon2 register enables pull-ups on all pins which also have their corresponding wpua bit set. when set, the rabpu bit disables all weak pull-ups. the weak pull-up is automatically turned off when the port pin is configured as an output. the pull-ups are disabled on a power-on reset. ra3 is an input only pin. its operation is controlled by the mclre bit of the config3h register. when selected as a port pin (mclre = 0 ), it functions as a digital input only pin; as such, it does not have tris or lat bits associated with its operation. pins ra4 and ra5 are multiplexed with the main oscillator pins; they are enabled as oscillator or i/o pins by the selection of the main oscillator in the configuration register (see section 23.1 ?configuration bits? for details). when they are not used as port pins, ra4 and ra5 and their associated tris and lat bits read as 0 . ra<4,2:0> are pins multiplexed with analog inputs. the operation of pins ra<4,2:0> as analog are selected by setting the ans<3:0> bits in the ansel register, which is the default setting after a power-on reset. example 8-1: initiali zing porta note 1: if a change on the i/o pin should occur when the read operation is being executed (start of the q2 cycle), then the rabif interrupt flag may not get set. furthermore, since a read or write on a port affects all bits of that port, care must be taken when using multiple pins in interrupt-on-change mode. changes on one pin may not be seen while servicing changes on another pin. note: on a power-on reset, ra3 is enabled as a digital input only if master clear functionality is disabled. clrf porta ; initialize porta by ; clearing output; data latches clrf lata ; alternate method ; to clear output; data latches movlw 030h ; value used to ; initialize data ; direction movwf trisa ; set ra<5:4> as output downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 75 pic18(l)f1xk22 register 8-1: porta: porta register u-0 u-0 r/w-x r/w-x r-x r/w-x r/w-x r/w-x ra5 ra4 ra3 ra2 ra1 ra0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-6 unimplemented : read as 0 bit 5-0 ra<5:0> : porta i/o pin bit (1) 1 = port pin is > v ih 0 = port pin is < v il note 1: the ra3 bit is only available when master clear reset is disabled (mclre configuration bit = 0 ). otherwise, ra3 reads as 0 . this bit is read-only. register 8-2: trisa: porta tri-state register u-0 u-0 r/w-1 r/w-1 u-1 r/w-1 r/w-1 r/w-1 trisa5 trisa4 trisa2 trisa1 trisa0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-6 unimplemented : read as 0 bit 5-4 trisa<5:4>: porta tri-state control bit (1) 1 = porta pin configured as an input (tri-stated) 0 = porta pin configured as an output bit 3 unimplemented : read as 1 bit 2-0 trisa<2:0>: porta tri-state control bit (1) 1 = porta pin configured as an input (tri-stated) 0 = porta pin configured as an output note 1: trisa<5:4> always reads 1 in xt, hs and lp oscillator modes. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 76 ? 2009-2016 microchip technology inc. register 8-3: lata: porta data latch register u-0 u-0 r/w-x r/w-x u-0 r/w-x r/w-x r/w-x l a t a 5l a t a 4 lata2 lata1 lata0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-6 unimplemented : read as 0 bit 5-4 lata<5:4> : ra<5:4> port i/o output latch register bits bit 3 unimplemented : read as 0 bit 2-0 lata<2:0> : ra<2:0> port i/o output latch register bits register 8-4: wpua: weak pull-up porta register u-0 u-0 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 wpua5 wpua4 wpua3 (1) wpua2 wpua1 wpua0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-6 unimplemented : read as 0 bit 5-0 wpua<5:0>: weak pull-up enable bit 1 = pull-up enabled 0 = pull-up disabled note 1: for the wpua3 bit, when mclre = 1 , weak pull-up is internally enabled, but not reported here. register 8-5: ioca: interrupt-on-change porta register u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ioca5 ioca4 ioca3 ioca2 ioca1 ioca0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-6 unimplemented : read as 0 bit 5-0 ioca<5:0> : porta i/o pin bit 1 = interrupt-on-change enabled 0 = interrupt-on-change disabled downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 77 pic18(l)f1xk22 table 8-1: porta i/o summary pin function tris setting i/o i/o type description ra0/an0/cv ref / dac1out/v ref -/ c1in+/int0/pgd ra0 0 o dig lata<0> data output. 1 i ttl porta<0> data input; programmable weak pull-up. an0 1 i ana adc channel 0 input. cv ref / dac1out x o ana dac reference voltage output. v ref - 1 i ana adc and dac reference voltage (low) input. c1in+ 1 i dig comparator c1 noninverting input. int0 1 i st external interrupt 0. pgd x o dig serial execution data output for icsp?. x i st serial execution data input for icsp. ra1/an1/c12in0-/ v ref +/int1/pgc ra1 0 o dig lata<1> data output. 1 i ttl porta<1> data input; programmable weak pull-up. an1 1 i ana adc channel 1. c12in0- 1 i ana comparator c1 and c2 inverting input channel 0. v ref + 1 i ana adc and dac reference voltage (high) input int1 1 st external interrupt 1. pgc x o dig serial execution clock output for icsp?. x i st serial execution clock input for icsp. ra2/an2/c1out/ t0cki/int2/srq ra2 0 o dig lata<2> data output. 1 i ttl porta<2> data input; programmable weak pull-up. an2 1 i ana adc channel 2. c1out 0 o dig comparator c1 output. t0cki 1 i st timer0 external clock input. int2 1 i st external interrupt 2. srq 0 o dig sr latch output. ra3/mclr /v pp ra3 (1) is t porta<37> data input; programmable weak pull-up. mclr is t active-low master clear with internal pull-up. v pp ia n a high-voltage programming input. ra4/an3/osc2/ clkout ra4 0 o dig lata<4> data output. 1 i ttl porta<4> data input; programmable weak pull-up. an3 1 ia n a a/d input channel 3. osc2 x oa n a main oscillator feedback output connection (xt, hs and lp modes). clkout x od i g system instruction cycle clock output. ra5/osc1/clkin/ t13cki ra5 0 o dig lata<5> data output. 1 i ttl porta<5> data input; programmable weak pull-up. osc1 x i ana main oscillator input connection. clkin x i ana main clock input connection. t13cki 1 is t timer1 and timer3 external clock input. legend: dig = digital level output; ttl = ttl input buffer; st = schmitt trigger input buffer; ana = analog level input/output; x = dont care (tris bit does not affect port direction or is overridden for this option). note 1: ra3 does not have a corresponding trisa bit. this pin is always an input regardless of mode. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 78 ? 2009-2016 microchip technology inc. table 8-2: summary of registers associated with porta name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page ansel ans7 ans6 ans5 ans4 ans3 ans2 ans1 ans0 247 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 244 intcon2 rabpu intedg0 intedg1 intedg2 tmr0ip rabip 244 ioca i o c a 5i o c a 4 ioca3 (2) ioca2 ioca1 ioca0 247 lata l a t a 5 (1) lata4 (1) lata2 lata1 lata0 247 porta ra5 (1) ra4 (1) ra3 (2) ra2 ra1 ra0 247 slrcon slrc slrb slra 247 trisa trisa5 (1) trisa4 (1) (3) trisa2 trisa1 trisa0 247 wpua w p u a 5w p u a 4 wpua3 (2) wpua2 wpua1 wpua0 244 legend: = unimplemented, read as 0 . shaded cells are not used by porta. note 1: ra<5:4> and their associated latch and data direction bits are enabled as i/o pins based on oscillator configuration; otherwise, they are read as 0 . 2: implemented only when master clear functionality is disabled (mclre configuration bit = 0 ). 3: unimplemented, read as 1 . downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 79 pic18(l)f1xk22 8.2 portb, trisb and latb registers portb is an 4-bit wide, bidirectional port. the corresponding data direction register is trisb. setting a trisb bit (= 1 ) will make the corresponding portb pin an input (i.e., disable the output driver). clearing a trisb bit (= 0 ) will make the corresponding portb pin an output (i.e., enable the output driver and put the contents of the output latch on the selected pin). the portb data latch register (latb) is also memory mapped. read-modify-write operations on the latb register read and write the latched output value for portb. example 8-2: initializing portb all portb pins are individually configurable as interrupt-on-change pins. control bits in the iocb register enable (when set) or disable (when clear) the interrupt function for each pin. when set, the rabie bit of the intcon register enables interrupts on all pins which also have their corresponding iocb bit set. when clear, the rabie bit disables all interrupt-on-changes. only pins configured as inputs can cause this interrupt to occur (i.e., any pin configured as an output is excluded from the interrupt-on-change comparison). for enabled interrupt-on-change pins, the values are compared with the old value latched on the last read of portb. the mismatch outputs of the last read are ord together to set the portb change interrupt flag bit (rabif) in the intcon register. this interrupt can wake the device from the sleep mode, or any of the idle modes. the user, in the interrupt service routine, can clear the interrupt in the following manner: a) any read or write of portb to clear the mismatch condition (except when portb is the source or destination of a movff instruction). b) clear the flag bit, rabif. a mismatch condition will continue to set the rabif flag bit. reading or writing portb will end the mismatch condition and allow the rabif bit to be cleared. the latch holding the last read value is not affected by a mclr nor brown-out reset. after either one of these resets, the rabif flag will continue to be set if a mismatch is present. the interrupt-on-change feature is recommended for wake-up on key depression operation and operations where portb is only used for the interrupt-on-change feature. polling of portb is not recommended while using the interrupt-on-change feature. all portb pins have individually controlled weak internal pull-up. when set, each bit of the wpub register enables the corresponding pin pull-up. when cleared, the rabpu bit of the intcon2 register enables pull-ups on all pins which also have their corresponding wpub bit set. when set, the rabpu bit disables all weak pull-ups. the weak pull-up is automatically turned off when the port pin is configured as an output. the pull-ups are disabled on a power-on reset. clrf portb ; initialize portb by ; clearing output ; data latches clrf latb ; alternate method ; to clear output ; data latches movlw 0f0h ; value used to ; initialize data ; direction movwf trisb ; set rb<7:4> as outputs note: if a change on the i/o pin should occur when the read operation is being executed (start of the q2 cycle), then the rabif interrupt flag may not get set. furthermore, since a read or write on a port affects all bits of that port, care must be taken when using multiple pins in interrupt-on-change mode. changes on one pin may not be seen while servicing changes on another pin. note: on a power-on reset, rb<5:4> are configured as analog inputs by default and read as 0 . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 80 ? 2009-2016 microchip technology inc. register 8-6: port b: portb register r/w-x r/w-x r/w-x r/w-x u-0 u-0 u-0 u-0 rb7 rb6 rb5 rb4 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-4 rb<7:4> : portb i/o pin bit 1 = port pin is >v ih 0 = port pin is : portb tri-state control bit 1 = portb pin configured as an input (tri-stated) 0 = portb pin configured as an output bit 3-0 unimplemented : read as 0 register 8-8: latb: portb data latch register r/w-x r/w-x r/w-x r/w-x u-0 u-0 u-0 u-0 latb7 latb6 latb5 latb4 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-4 latb<7:4> : rb<7:4> port i/o output latch register bits bit 3-0 unimplemented : read as 0 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 81 pic18(l)f1xk22 register 8-9: wpub: weak pull-up portb register r/w-1 r/w-1 r/w-1 r/w-1 u-0 u-0 u-0 u-0 wpub7 wpub6 wpub5 wpub4 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-4 wpub<7:4>: weak pull-up enable bit 1 = pull-up enabled 0 = pull-up disabled bit 3-0 unimplemented : read as 0 register 8-10: iocb: interrup t-on-change portb register r/w-0 r/w-0 r/w-0 r/w-0 u-0 u-0 u-0 u-0 iocb7 iocb6 iocb5 iocb4 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-4 iocb<7:4> : interrupt-on-change bits 1 = interrupt-on-change enabled 0 = interrupt-on-change disabled bit 3-0 unimplemented : read as 0 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 82 ? 2009-2016 microchip technology inc. table 8-3: portb i/o summary pin function tris setting i/o i/o type description rb4/an10/sdi/ sda rb4 0 o dig latb<4> data output. 1 i ttl portb<4> data input; programmable weak pull-up. an10 1 i ana adc input channel 10. sdi 1 i st spi data input (mssp module). sda 1 od i gi 2 c data output (mssp module). 1 ii 2 ci 2 c data input (mssp module); input type depends on module setting. rb5/an11/rx/dt rb5 0 o dig latb<5> data output. 1 i ttl portb<5> data input; programmable weak pull-up. an11 1 i ana adc input channel 11. rx 1 i st asynchronous serial receive data input (usart module). dt 1 od i g synchronous serial data output (usart module); takes priority over port data. 1 is t synchronous serial data input (usart module). user must configure as an input. rb6/sck/scl rb6 0 o dig latb<6> data output. 1 i ttl portb<6> data input; programmable weak pull-up. sck 0 o dig spi clock output (mssp module). 1 i st spi clock input (mssp module). scl 0 od i gi 2 c clock output (mssp module). 1 ii 2 ci 2 c clock input (mssp module); input type depends on module setting. rb7/tx/ck rb7 0 o dig latb<7> data output. 1 i ttl portb<7> data input; programmable weak pull-up. tx 1 od i g asynchronous serial transmit data output (usart module). ck 1 od i g synchronous serial clock output (usart module). 1 i st synchronous serial clock input (usart module). legend: dig = digital level output; ttl = ttl input buffer; st = schmitt trigger input buffer; ana = analog level input/output; i 2 c = schmitt trigger input with i 2 c; x = dont care (tris bit does not affect port direction or is ove rridden for this option). downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 83 pic18(l)f1xk22 table 8-4: summary of registers associated with portb name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page anselh ans11 ans10 ans9 ans8 247 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 244 intcon2 rabpu intedg0 intedg1 intedg2 tmr0ip r a b i p 244 iocb iocb7 iocb6 iocb5 iocb4 247 latb latb7 latb6 latb5 latb4 247 portb rb7 rb6 rb5 rb4 247 rcsta spen rx9 sren cren adden ferr oerr rx9d 246 slrcon slrc slrb slra 247 sspcon1 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 245 trisb trisb7 trisb6 trisb5 trisb4 247 txsta csrc tx9 txen sync sendb brgh trmt tx9d 246 wpub wpub7 wpub6 wpub5 wpub4 247 legend: = unimplemented, read as 0 . shaded cells are not used by portb. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 84 ? 2009-2016 microchip technology inc. 8.3 portc, trisc and latc registers portc is an 8-bit wide, bidirectional port. the corresponding data direction register is trisc. setting a trisc bit (= 1 ) will make the corresponding portc pin an input (i.e., disable the output driver). clearing a trisc bit (= 0 ) will make the corresponding portc pin an output (i.e., enable the output driver and put the contents of the output latch on the selected pin). the portc data latch register (latc) is also memory mapped. read-modify-write operations on the latc register read and write the latched output value for portc. all the pins on portc are implemented with schmitt trigger input buffer. each pin is individually configurable as an input or output. example 8-3: initializing portc note: on a power-on reset, rc<7:6> and rc<3:0> are configured as analog inputs and read as 0 . clrf portc ; initialize portc by ; clearing output; data latches clrf latc ; alternate method ; to clear output; data latches movlw 0cfh ; value used to ; initialize data ; direction movwf trisc ; set rc<3:0> as inputs ; rc<5:4> as outputs; rc<7:6> as inputs register 8-11: portc: portc register r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x rc7 rc6 rc5 rc4 rc3 rc2 rc1 rc0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-0 rc<7:0> : portc i/o pin bits 1 = port pin is > v ih 0 = port pin is < v il register 8-12: trisc: po rtc tri-state register r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-0 trisc<7:0>: portc tri-state control bits 1 = portc pin configured as an input (tri-stated) 0 = portc pin configured as an output downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 85 pic18(l)f1xk22 register 8-13: latc: portc data latch register r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x latc7 latc6 latc5 latc4 latc3 latc2 latc1 latc0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-0 latc<7:0> : rb<7:0> port i/o output latch register bits downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 86 ? 2009-2016 microchip technology inc. table 8-5: portc i/o summary pin function tris setting i/o i/o type description rc0/an4/c2in+ rc0 0 o dig latc<0> data output. 1 i st portc<0> data input. an4 1 i ana a/d input channel 4. c2in+ 1 i ana comparators c2 noninverting input. rc1/an5/ c12in1- rc1 0 o dig latc<1> data output. 1 i st portc<1> data input. an5 1 i ana a/d input channel 5. c12in1- 1 i ana comparators c1 and c2 inverting input, channel 1. rc2/an6/ c12in2-/p1d rc2 0 o dig latc<2> data output. 1 i st portc<2> data input. an6 1 i ana a/d input channel 6. c12in2- 1 i ana comparators c1 and c2 inverting input, channel 2. p1d 0 o dig eccp1 enhanced pwm output, channel d. rc3/an7/ c12in3-/p1c/ pgm rc3 0 o dig latc<3> data output. 1 i st portc<3> data input. an7 1 i ana a/d input channel 7. c12in3- 1 i ana comparators c1 and c2 inverting input, channel 3. p1c 0 o dig eccp1 enhanced pwm output, channel c. pgm x i st single-supply programming mode entry (icsp?). enabled by lvp configuration bit; all other pin functions disabled. rc4/c2out/p1b/ srnq rc4 0 o dig latc<4> data output. 1 i st portc<4> data input. c2out 0 o dig comparator 2 output. p1b 0 o dig eccp1 enhanced pwm output, channel b. srnq 0 o dig sr latch inverted output rc5/ccp1/p1a rc5 0 o dig latc<5> data output. 1 i st portc<5> data input. ccp1 0 o dig eccp1 compare or pwm output. 1 i st eccp1 capture input. p1a 0 0 dig eccp1 enhanced pwm output, channel a. rc6/an8/ss rc6 0 o dig latc<6> data output. 1 i st portc<6> data input. an8 1 i ana a/d input channel 8. ss 1 i ttl slave select input for ssp (mssp module) rc7/an9/sdo rc7 0 o dig latc<7> data output. 1 i st portc<7> data input. an9 1 i ana a/d input channel 9. sdo 0 o dig spi data output (mssp module). legend: dig = digital level output; ttl = ttl input buffer; st = schmitt trigger input buffer; ana = analog level input/output; x = dont care (tris bit does not affect port direction or is overridden for this option). downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 87 pic18(l)f1xk22 table 8-6: summary of registers associated with portc name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page ansel ans7 ans6 ans5 ans4 ans3 ans2 ans1 ans0 247 anselh ans11 ans10 ans9 ans8 247 ccp1con p1m1 p1m0 dc1b1 dc1b0 ccp1m3 ccp1m2 ccp1m1 ccp1m0 246 eccp1as eccpase eccpas2 eccpas1 eccpas0 pssac1 pssac0 pssbd1 pssbd0 246 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 244 intcon2 rabpu intedg0 intedg1 intedg2 tmr0ip rabip 244 intcon3 int2ip int1ip i n t 2 i e int1ie i n t 2 i f int1if 244 latc latc7 latc6 latc5 latc4 latc3 latc2 latc1 latc0 247 portc rc7 rc6 rc5 rc4 rc3 rc2 rc1 rc0 247 pstrcon strsync strd strc strb stra 246 vrefcon1 d1en d1lps dac1oe --- d1pss1 d1pss0 --- d1nss 246 slrcon s l r c slrb slra 247 sspcon1 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 245 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 247 t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 245 t3con rd16 t3ckps1 t3ckps0 t3ccp1 t3sync tmr3cs tmr3on 246 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 88 ? 2009-2016 microchip technology inc. 8.4 port analog control some port pins are multiplexed with analog functions such as the analog-to-digital converter and comparators. when these i/o pins are to be used as analog inputs it is necessary to disable the digital input buffer to avoid excessive current caused by improper biasing of the digital input. individual control of the digital input buffers on pins which share analog functions is provided by the ansel and anselh registers. setting an ansx bit high will disable the associated digital input buffer and cause all reads of that pin to return 0 while allowing analog functions of that pin to operate correctly. the state of the ansx bits has no affect on digital output functions. a pin with the associated trisx bit clear and ansx bit set will still operate as a digital output but the input mode will be analog. register 8-14: ansel: analog select register r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 ans7 ans6 ans5 ans4 ans3 ans2 ans1 ans0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 ans7: rc3 analog select control bit 1 = digital input buffer of rc3 is disabled 0 = digital input buffer of rc3 is enabled bit 6 ans6: rc2 analog select control bit 1 = digital input buffer of rc2 is disabled 0 = digital input buffer of rc2 is enabled bit 5 ans5: rc1 analog select control bit 1 = digital input buffer of rc1 is disabled 0 = digital input buffer of rc1 is enabled bit 4 ans4: rc0 analog select control bit 1 = digital input buffer of rc0 is disabled 0 = digital input buffer of rc0 is enabled bit 3 ans3: ra4 analog select control bit 1 = digital input buffer of ra4 is disabled 0 = digital input buffer of ra4 is enabled bit 2 ans2: ra2 analog select control bit 1 = digital input buffer of ra2 is disabled 0 = digital input buffer of ra2 is enabled bit 1 ans1: ra1 analog select control bit 1 = digital input buffer of ra1 is disabled 0 = digital input buffer of ra1 is enabled bit 0 ans0: ra0 analog select control bit 1 = digital input buffer of ra0 is disabled 0 = digital input buffer of ra0 is enabled downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 89 pic18(l)f1xk22 register 8-15: anselh: analog select high register u-0 u-0 u-0 u-0 r/w-1 r/w-1 r/w-1 r/w-1 ans11 ans10 ans9 ans8 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-4 unimplemented: read as 0 bit 3 ans11: rb5 analog select control bit 1 = digital input buffer of rb5 is disabled 0 = digital input buffer of rb5 is enabled bit 2 ans10: rb4 analog select control bit 1 = digital input buffer of rb4 is disabled 0 = digital input buffer of rb4 is enabled bit 1 ans9: rc7 analog select control bit 1 = digital input buffer of rc7 is disabled 0 = digital input buffer of rc7 is enabled bit 0 ans8: rc6 analog select control bit 1 = digital input buffer of rc6 is disabled 0 = digital input buffer of rc6 is enabled downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 90 ? 2009-2016 microchip technology inc. 8.5 port slew rate control the output slew rate of each port is programmable to select either the standard transition rate or a reduced transition rate of 0.1 times the standard to minimize emi. the reduced transition time is the default slew rate for all ports. register 8-16: slrcon: sl ew rate control register u-0 u-0 u-0 u-0 u-0 r/w-1 r/w-1 r/w-1 slrc slrb slra bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-3 unimplemented: read as 0 bit 2 slrc: portc slew rate control bit 1 = all outputs on portc slew at 0.1 times the standard rate 0 = all outputs on portc slew at the standard rate bit 1 slrb: portb slew rate control bit 1 = all outputs on portb slew at 0.1 times the standard rate 0 = all outputs on portb slew at the standard rate bit 0 slra: porta slew rate control bit 1 = all outputs on porta slew at 0.1 times the standard rate ( 1 ) 0 = all outputs on porta slew at the standard rate note 1: the slew rate of ra4 defaults to standard rate when the pin is used as clkout. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 91 pic18(l)f1xk22 9.0 timer0 module the timer0 module incorporates the following features: software selectable operation as a timer or counter in both 8-bit or 16-bit modes readable and writable registers dedicated 8-bit, software programmable prescaler selectable clock source (internal or external) edge select for external clock interrupt-on-overflow the t0con register ( register 9-1 ) controls all aspects of the modules operation, including the prescale selection. it is both readable and writable. a simplified block diagram of the timer0 module in 8-bit mode is shown in figure 9-1 . figure 9-2 shows a simplified block diagram of the timer0 module in 16-bit mode. register 9-1: t0con: ti mer0 control register r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 tmr0on t08bit t0cs t0se psa t0ps2 t0ps1 t0ps0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 tmr0on: timer0 on/off control bit 1 = enables timer0 0 = stops timer0 bit 6 t08bit : timer0 8-bit/16-bit control bit 1 = timer0 is configured as an 8-bit timer/counter 0 = timer0 is configured as a 16-bit timer/counter bit 5 t0cs : timer0 clock source select bit 1 = transition on t0cki pin 0 = internal instruction cycle clock (clkout) bit 4 t0se : timer0 source edge select bit 1 = increment on high-to-low transition on t0cki pin 0 = increment on low-to-high transition on t0cki pin bit 3 psa : timer0 prescaler assignment bit 1 = timer0 prescaler is not assigned. timer0 clock input bypasses presca ler. 0 = timer0 prescaler is assigned. timer0 clock input comes from prescaler out put. bit 2-0 t0ps<2:0> : timer0 prescaler select bits 111 = 1:256 prescale value 110 = 1:128 prescale value 101 = 1:64 prescale value 100 = 1:32 prescale value 011 = 1:16 prescale value 010 = 1:8 prescale value 001 = 1:4 prescale value 000 = 1:2 prescale value downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 92 ? 2009-2016 microchip technology inc. 9.1 timer0 operation timer0 can operate as either a timer or a counter; the mode is selected with the t0cs bit of the t0con register. in timer mode (t0cs = 0 ), the module increments on every clock by default unless a different prescaler value is selected (see section 9.3 ?prescaler? ). timer0 incrementing is inhibited for two instruction cycles following a tmr0 register write. the user can work around this by adjusting the value written to the tmr0 register to compensate for the anticipated missing increments. the counter mode is selected by setting the t0cs bit (= 1 ). in this mode, timer0 increments either on every rising or falling edge of the t0cki pin. the incrementing edge is determined by the timer0 source edge select bit, t0se of the t0con register; clearing this bit selects the rising edge. restrictions on the external clock input are discussed below. an external clock source can be used to drive timer0; however, it must meet certain requirements (see table 26-17 ) to ensure that the external clock can be synchronized with the internal phase clock (t osc ). there is a delay between synchronization and the onset of incrementing the timer/counter. 9.2 timer0 reads and writes in 16-bit mode tmr0h is not the actual high byte of timer0 in 16-bit mode; it is actually a buffered version of the real high byte of timer0 which is neither directly readable nor writable (refer to figure 9-2 ). tmr0h is updated with the contents of the high byte of timer0 during a read of tmr0l. this provides the ability to read all 16 bits of timer0 without the need to verify that the read of the high and low byte were valid. invalid reads could otherwise occur due to a rollover between successive reads of the high and low byte. similarly, a write to the high byte of timer0 must also take place through the tmr0h buffer register. writing to tmr0h does not directly affect timer0. instead, the high byte of timer0 is updated with the contents of tmr0h when a write occurs to tmr0l. this allows all 16 bits of timer0 to be updated at once. figure 9-1: timer0 block diagram (8-bit mode) note: upon reset, timer0 is enabled in 8-bit mode with clock input from t0cki. tmr0 prescal er is set to maximum (1:256), but on reset is not assigned to the timer. t0cki pin t0se 0 1 1 0 t0cs f osc /4 programmable prescaler sync with internal clocks tmr0l (2 t cy delay) internal data bus psa t0ps<2:0> set tmr0if on overflow 3 8 8 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 93 pic18(l)f1xk22 figure 9-2: timer0 block diagram (16-bit mode) 9.3 prescaler an 8-bit counter is available as a prescaler for the timer0 module. the prescaler is not directly readable or writable; its value is set by the psa and t0ps<2:0> bits of the t0con register which determine the prescaler assignment and prescale ratio. clearing the psa bit assigns the prescaler to the timer0 module. when the prescaler is assigned, prescale values from 1:2 through 1:256 in integer power-of-2 increments are selectable. when assigned to the timer0 module, all instructions writing to the tmr0 register (e.g., clrf tmr0 , movwf tmr0 , bsf tmr0 , etc.) clear the prescaler count. 9.3.1 switching prescaler assignment the prescaler assignment is fully under software control and can be changed on-the-fly during program execution. 9.4 timer0 interrupt the tmr0 interrupt is generated when the tmr0 register overflows from ffh to 00h in 8-bit mode, or from ffffh to 0000h in 16-bit mode. this overflow sets the tmr0if flag bit. the interrupt can be masked by clearing the tmr0ie bit of the intcon register. before re-enabling the interrupt, the tmr0if bit must be cleared by software in the interrupt service routine. since timer0 is shut down in sleep mode, the tmr0 interrupt cannot awaken the processor from sleep. table 9-1: registers associated with timer0 note: upon reset, timer0 is enabled in 8-bit mode with clock input from t0cki. tmr0 prescale r is set to maximum (1:256), but on reset is not assigned to the timer. t0cki pin t0se 0 1 1 0 t0cs f osc /4 programmable prescaler sync with internal clocks tmr0l (2 t cy delay) internal data bus 8 psa t0ps<2:0> set tmr0if on overflow 3 tmr0 tmr0h high byte 8 8 8 read tmr0l write tmr0l 8 note: writing to tmr0 when the prescaler is assigned to timer0 will clear the prescaler count but will not change the prescaler assignment. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 porta ra7 ra6 ra5 ra4 ra3 ra2 ra1 ra0 248 tmr0h timer0 register, high byte 246 tmr0l timer0 register, low byte 246 trisa trisa5 trisa4 (1) trisa2 trisa1 trisa0 248 t0con tmr0on t08bit t0cs t0se psa t0ps2 t0ps1 t0ps0 246 legend: shaded cells are not used by timer0. note: unimplemented, read as 1 . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 94 ? 2009-2016 microchip technology inc. 10.0 timer1 module the timer1 timer/counter module incorporates the following features: software selectable operation as a 16-bit timer or counter readable and writable 8-bit registers (tmr1h and tmr1l) selectable internal or external clock source and timer1 oscillator options interrupt-on-overflow reset on ccp special event trigger device clock status flag (t1run) a simplified block diagram of the timer1 module is shown in figure 10-1 . a block diagram of the modules operation in read/write mode is shown in figure 10-2 . timer1 can also be used to provide real-time clock (rtc) functionality to applications with only a minimal addition of external components and code overhead. timer1 is controlled through the t1con control register ( register 10-1 ). it also contains the timer1 oscillator enable bit (t1oscen). timer1 can be enabled or disabled by setting or clearing control bit, tmr1on of the t1con register. register 10-1: t1con: ti mer1 control register r/w-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 rd16: 16-bit read/write mode enable bit 1 = enables register read/write of timer1 in one 16-bit operation 0 = enables register read/write of timer1 in two 8-bit operations bit 6 t1run: timer1 system clock status bit 1 = main system clock is derived from timer1 oscillator 0 = main system clock is derived from another source bit 5-4 t1ckps<1:0>: timer1 input clock prescale select bits 11 = 1:8 prescale value 10 = 1:4 prescale value 01 = 1:2 prescale value 00 = 1:1 prescale value bit 3 t1oscen: timer1 oscillator enable bit 1 = timer1 oscillator is enabled 0 = timer1 oscillator is shut off the oscillator inverter and feedback resistor are turned off to eliminate power drain. bit 2 t1sync : timer1 external clock input synchronization select bit when tmr1cs = 1 : 1 = do not synchronize external clock input 0 = synchronize external clock input when tmr1cs = 0 : this bit is ignored. timer1 uses the internal clock when tmr1cs = 0 . bit 1 tmr1cs: timer1 clock source select bit 1 = external clock from the t13cki pin (on the rising edge) 0 = internal clock (f osc /4) bit 0 tmr1on: timer1 on bit 1 = enables timer1 0 = stops timer1 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 95 pic18(l)f1xk22 10.1 timer1 operation timer1 can operate in one of the following modes: timer synchronous counter asynchronous counter the operating mode is determined by the clock select bit, tmr1cs of the t1con register. when tmr1cs is cleared (= 0 ), timer1 increments on every internal instruction cycle (f osc /4). when the bit is set, timer1 increments on every rising edge of either the timer1 external clock input or the timer1 oscillator, if enabled. when the timer1 oscillator is enabled, the digital circuitry associated with the osc1 and osc2 pins is disabled. this means the values of trisa<5:4> are ignored and the pins are read as 0 . figure 10-1: timer1 block diagram t1sync tmr1cs t1ckps<1:0> sleep input f osc /4 internal clock on/off prescaler 1, 2, 4, 8 synchronize detect 10 2 osc1/t13cki osc2 10 tmr1on tmr1l set tmr1if on overflow tmr1 high byte clear tmr1 (ccp special event trigger) timer1 oscillator note 1: when enable bit, t1oscen, is cleared, the inverter and f eedback resistor are turned off to eliminate power drain. on/off timer1 timer1 clock input t1oscen (1) intosc without clkout downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 96 ? 2009-2016 microchip technology inc. figure 10-2: timer1 block diag ram (16-bit read/write mode) t1sync tmr1cs t1ckps<1:0> sleep input f osc /4 internal clock prescaler 1, 2, 4, 8 synchronize detect 10 2 osc1/t13cki osc2 note 1: when enable bit, t1oscen, is cleared, the inverter and f eedback resistor are turned off to eliminate power drain. 10 tmr1l internal data bus 8 set tmr1if on overflow tmr1 tmr1h high byte 8 8 8 read tmr1l write tmr1l 8 tmr1on clear tmr1 (ccp special event trigger) timer1 oscillator on/off timer1 timer1 clock input t1oscen (1) intosc without clkout downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 97 pic18(l)f1xk22 10.2 timer1 16-bit read/write mode timer1 can be configured for 16-bit reads and writes (see figure 10-2 ). when the rd16 control bit of the t1con register is set, the address for tmr1h is mapped to a buffer register for the high byte of timer1. a read from tmr1l will load the contents of the high byte of timer1 into the timer1 high byte buffer. this provides the user with the ability to accurately read all 16 bits of timer1 without the need to determine whether a read of the high byte, followed by a read of the low byte, has become invalid due to a rollover or carry between reads. writing to tmr1h does not directly affect timer1. instead, the high byte of timer1 is updated with the contents of tmr1h when a write occurs to tmr1l. this allows all 16 bits of timer1 to be updated at once. the high byte of timer1 is not directly readable or writable in this mode. all reads and writes must take place through the timer1 high byte buffer register. writes to tmr1h do not clear the timer1 prescaler. the prescaler is only cleared on writes to tmr1l. 10.3 clock source selection the tmr1cs bit of the t1con register is used to select the clock source. when tmr1cs = 0 , the clock source is f osc /4. when tmr1cs = 1 , the clock source is supplied externally. 10.3.1 internal clock source when the internal clock source is selected the tmr1h:tmr1l register pair will increment on multiples of f osc as determined by the timer1 prescaler. 10.3.2 external clock source when the external clock source is selected, the timer1 module may work as a timer or a counter. when counting, timer1 is incremented on the rising edge of the external clock input t1cki. in addition, the counter mode clock can be synchronized to the microcontroller system clock or run asynchronously. if an external clock oscillator is needed (and the microcontroller is using the intosc without clkout), timer1 can use the lp oscillator as a clock source. 10.4 timer1 oscillator an on-chip crystal oscillator circuit is incorporated between pins osc1 (input) and osc2 (amplifier output). it is enabled by setting the timer1 oscillator enable bit, t1oscen of the t1con register. the oscillator is a low-power circuit rated for 32 khz crystals. it will continue to run during all power-managed modes. the circuit for a typical lp oscillator is shown in figure 10-3 . table 10-1 shows the capacitor selection for the timer1 oscillator. the timer1 oscillator is shared with the system lp oscillator. thus, timer1 can use this mode only when the primary system clock is derived from the internal oscillator or when the oscillator is in the lp mode. the user must provide a software time delay to ensure proper oscillator start-up. figure 10-3: external components for the timer1 lp oscillator clock source t1oscen fosc mode tmr1cs f osc /4 x xxx 0 t1cki pin 0 xxx 1 t1lposc 1 lp or intoscio 1 note: in counter mode, a falling edge must be registered by the counter prior to the first incrementing rising edge after any one or more of the following conditions: timer1 enabled after por write to tmr1h or tmr1l timer1 is disabled timer1 is disabled (tmr1on 0 ) when t1cki is high then timer1 is enabled (tmr1on= 1 ) when t1cki is low. note: see figure 9-2 . note: see the notes with table 10-1 for additional information about capacitor selection. c1 c2 xtal osc1 osc2 32.768 khz 27 pf 27 pf pic ? mcu downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 98 ? 2009-2016 microchip technology inc. table 10-1: capacitor selection for the timer oscillator 10.5 timer1 interrupt the tmr1 register pair (tmr1h:tmr1l) increments from 0000h to ffffh and rolls over to 0000h. the timer1 interrupt, if enabled, is generated on overflow, which is latched in the tmr1if interrupt flag bit of the pir1 register. this interrupt can be enabled or disabled by setting or clearing the tmr1ie interrupt enable bit of the pie1 register. 10.6 resetting timer1 using the ccp special event trigger if either of the ccp modules is configured to use timer1 and generate a special event trigger in compare mode (ccp1m<3:0> or ccp2m<3:0> = 1011 ), this signal will reset timer1. the trigger from ccp2 will also start an a/d conversion if the a/d module is enabled (see section 13.3.4 ?special event trigger? for more information). the module must be configured as either a timer or a synchronous counter to take advantage of this feature. when used this way, the ccprh:ccprl register pair effectively becomes a period register for timer1. if timer1 is running in asynchronous counter mode, this reset operation may not work. in the event that a write to timer1 coincides with a special event trigger, the write operation will take precedence. 10.7 using timer1 as a real-time clock adding an external lp oscillator to timer1 (such as the one described in section 10.4 ?timer1 oscillator? above) gives users the option to include rtc functionality to their applications. this is accomplished with an inexpensive watch crystal to provide an accurate time base and several lines of application code to calculate the time. when operating in sleep mode and using a battery or supercapacitor as a power source, it can completely eliminate the need for a separate rtc device and battery backup. the application code routine, rtcisr , shown in example 10-1 , demonstrates a simple method to increment a counter at one-second intervals using an interrupt service routine. incrementing the tmr1 register pair to overflow triggers the interrupt and calls the routine, which increments the seconds counter by one; additional counters for minutes and hours are incremented on overflows of the less significant counters. since the register pair is 16-bit wide, a 32.768 khz clock source will take two seconds to count up to overflow. to force the overflow at the required one-second intervals, it is necessary to pre-load it; the simplest method is to set the msb of tmr1h with a bsf instruction. note that the tmr1l register is never preloaded or altered; doing so may introduce cumulative error over many cycles. for this method to be accurate, timer1 must operate in asynchronous mode and the timer1 overflow interrupt must be enabled (pie1<0> = 1 ), as shown in the routine, rtcinit . the timer1 oscillator must also be enabled and running at all times. osc type freq. c1 c2 lp 32 khz 27 pf (1) 27 pf (1) note 1: microchip suggests these values only as a starting point in validating the oscillator circuit. 2: higher capacitance increases the stability of the oscillator but also increases the start-up time. 3: since each resonator/crystal has its own characteristics, the user should consult the resonator/crystal manufacturer for appropriate values of external components. 4: capacitor values are for design guidance only. note: the special event triggers from the ccp2 module will not set the tmr1if interrupt flag bit of the pir1 register. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 99 pic18(l)f1xk22 example 10-1: implementing a real-time clock using a timer1 interrupt service table 10-2: registers associated with timer1 as a timer/counter rtcinit movlw 80h ; preload tmr1 register pair movwf tmr1h ; for 1 second overflow clrf tmr1l movlw b00001111 ; configure for external clock, movwf t1con ; asynchronous operation, external oscillator clrf secs ; initialize timekeeping registers clrf mins ; movlw .12 movwf hours bsf pie1, tmr1ie ; enable timer1 interrupt return rtcisr bsf tmr1h, 7 ; preload for 1 sec overflow bcf pir1, tmr1if ; clear interrupt flag incf secs, f ; increment seconds movlw .59 ; 60 seconds elapsed? cpfsgt secs return ; no, done clrf secs ; clear seconds incf mins, f ; increment minutes movlw .59 ; 60 minutes elapsed? cpfsgt mins return ; no, done clrf mins ; clear minutes incf hours, f ; increment hours movlw .23 ; 24 hours elapsed? cpfsgt hours return ; no, done clrf hours ; reset hours return ; done name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr1 adip rcip txip sspip ccp1ip tmr2ip tmr1ip 248 pie1 adie rcie txie sspie ccp1ie tmr2ie tmr1ie 248 pir1 adif rcif txif sspif ccp1if tmr2if tmr1if 248 tmr1h timer1 register, high byte 246 tmr1l timer1 register, low byte 246 trisa trisa5 trisa4 (1) trisa2 trisa1 trisa0 248 t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 246 legend: = unimplemented, read as 0 . shaded cells are not used by the timer1 module. note 1: unimplemented, read as 1 . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 100 ? 2009-2016 microchip technology inc. 11.0 timer2 module the timer2 module timer incorporates the following features: 8-bit timer and period registers (tmr2 and pr2, respectively) readable and writable (both registers) software programmable prescaler (1:1, 1:4 and 1:16) software programmable postscaler (1:1 through 1:16) interrupt on tmr2-to-pr2 match optional use as the shift clock for the mssp module the module is controlled through the t2con register ( register 11-1 ), which enables or disables the timer and configures the prescaler and postscaler. timer2 can be shut off by clearing control bit, tmr2on of the t2con register, to minimize power consumption. a simplified block diagram of the module is shown in figure 11-1 . 11.1 timer2 operation in normal operation, tmr2 is incremented from 00h on each clock (f osc /4). a 4-bit counter/prescaler on the clock input gives direct input, divide-by-4 and divide-by-16 prescale options; these are selected by the prescaler control bits, t2ckps<1:0> of the t2con register. the value of tmr2 is compared to that of the period register, pr2, on each clock cycle. when the two values match, the comparator generates a match signal as the timer output. this signal also resets the value of tmr2 to 00h on the next cycle and drives the output counter/postscaler (see section 11.2 ?timer2 interrupt? ). the tmr2 and pr2 registers are both directly readable and writable. the tmr2 register is cleared on any device reset, whereas the pr2 register initializes to ffh. both the prescaler and postscaler counters are cleared on the following events: a write to the tmr2 register a write to the t2con register any device reset (power-on reset, mclr reset, watchdog timer reset or brown-out reset) tmr2 is not cleared when t2con is written. register 11-1: t2con: timer2 control register u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 t2outps3 t2outps2 t2outps1 t2outps0 tmr2on t2ckps1 t2ckps0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 unimplemented: read as 0 bit 6-3 t2outps<3:0> : timer2 output postscale select bits 0000 = 1:1 postscale 0001 = 1:2 postscale 1111 = 1:16 postscale bit 2 tmr2on : timer2 on bit 1 = timer2 is on 0 = timer2 is off bit 1-0 t2ckps<1:0> : timer2 clock prescale select bits 00 = prescaler is 1 01 = prescaler is 4 1x = prescaler is 16 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 101 pic18(l)f1xk22 11.2 timer2 interrupt timer2 can also generate an optional device interrupt. the timer2 output signal (tmr2-to-pr2 match) provides the input for the 4-bit output counter/postscaler. this counter generates the tmr2 match interrupt flag which is latched in tmr2if of the pir1 register. the interrupt is enabled by setting the tmr2 match interrupt enable bit, tmr2ie of the pie1 register. a range of 16 postscale options (from 1:1 through 1:16 inclusive) can be selected with the postscaler control bits, t2outps<3:0> of the t2con register. 11.3 timer2 output the unscaled output of tmr2 is available primarily to the ccp modules, where it is used as a time base for operations in pwm mode. timer2 can be optionally used as the shift clock source for the mssp module operating in spi mode. additional information is provided in section 14.0 ?master synchronous serial port (mssp) module? . figure 11-1: timer2 block diagram table 11-1: registers associated with timer2 as a timer/counter name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr1 adip rcip txip sspip ccp1ip tmr2ip tmr1ip 248 pie1 adie rcie txie sspie ccp1ie tmr2ie tmr1ie 248 pir1 adif rcif txif sspif ccp1if tmr2if tmr1if 248 pr2 timer2 period register 246 tmr2 timer2 register 246 t2con t2outps3 t2outps2 t2outps1 t2o utps0 tmr2on t2ckps1 t2ckps0 246 legend: = unimplemented, read as 0 . shaded cells are not used by the timer2 module. comparator tmr2 output tmr2 postscaler prescaler pr2 2 f osc /4 1:1 to 1:16 1:1, 1:4, 1:16 4 t2outps<3:0> t2ckps<1:0> set tmr2if internal data bus 8 reset tmr2/pr2 8 8 (to pwm or mssp) match downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 102 ? 2009-2016 microchip technology inc. 12.0 timer3 module the timer3 module timer/counter incorporates these features: software selectable operation as a 16-bit timer or counter readable and writable 8-bit registers (tmr3h and tmr3l) selectable clock source (internal or external) with device clock or timer1 oscillator internal options interrupt-on-overflow module reset on ccp special event trigger a simplified block diagram of the timer3 module is shown in figure 12-1 . a block diagram of the modules operation in read/write mode is shown in figure 12-2 . the timer3 module is controlled through the t3con register ( register 12-1 ). it also selects the clock source options for the ccp modules (see section 13.1.1 ?ccp module and timer resources? for more information). register 12-1: t3con: ti mer3 control register r/w-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 rd16 t3ckps1 t3ckps0 t3ccp1 t3sync tmr3cs tmr3on bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 rd16: 16-bit read/write mode enable bit 1 = enables register read/write of timer3 in one 16-bit operation 0 = enables register read/write of timer3 in two 8-bit operations bit 6 unimplemented: read as 0 bit 5-4 t3ckps<1:0> : timer3 input clock prescale select bits 11 = 1:8 prescale value 10 = 1:4 prescale value 01 = 1:2 prescale value 00 = 1:1 prescale value bit 3 t3ccp1: timer3 and timer1 to ccp1 enable bits 1 = timer3 is the clock source for compare/capture of eccp1 0 = timer1 is the clock source for compare/capture of eccp1 bit 2 t3sync : timer3 external clock input synchronization control bit (not usable if the device clock comes from timer1/timer3.) when tmr3cs = 1 : 1 = do not synchronize external clock input 0 = synchronize external clock input when tmr3cs = 0 : this bit is ignored. timer3 uses the internal clock when tmr3cs = 0 . bit 1 tmr3cs: timer3 clock source select bit 1 = external clock input from timer1 oscillator or t13cki (on the rising edge after the first falling edge) 0 = internal clock (f osc /4) bit 0 tmr3on: timer3 on bit 1 = enables timer3 0 = stops timer3 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 103 pic18(l)f1xk22 12.1 timer3 operation timer3 can operate in one of three modes: timer synchronous counter asynchronous counter the operating mode is determined by the clock select bit, tmr3cs of the t3con register. when tmr3cs is cleared (= 0 ), timer3 increments on every internal instruction cycle (f osc /4). when the bit is set, timer3 increments on every rising edge of the timer1 external clock input or the timer1 oscillator, if enabled. as with timer1, the digital circuitry associated with the osc1 and osc2 pins is disabled when the timer1 oscillator is enabled. this means the values of trisa<5:4> are ignored and the pins are read as 0 . figure 12-1: timer3 block diagram t3sync tmr3cs t3ckps<1:0> sleep input f osc /4 internal clock prescaler 1, 2, 4, 8 synchronize detect 10 2 osc1/t13cki osc2 10 tmr3on tmr3l set tmr3if on overflow tmr3 high byte timer1 oscillator note 1: when enable bit, t1oscen, is cleared, the inverter and feedback resistor are turned off to eliminat e power drain. on/off timer3 ccp1 special event trigger ccp1 select from t3con<3> clear tmr3 timer1 clock input t1oscen (1) intosc without clkout downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 104 ? 2009-2016 microchip technology inc. figure 12-2: timer3 block diag ram (16-bit read/write mode) t3sync tmr3cs t3ckps<1:0> sleep input f osc /4 internal clock prescaler 1, 2, 4, 8 synchronize detect 10 2 osc1/t1osi osc2 note 1: when enable bit, t1oscen, is cleared, the inverter and feedback resistor are turned off to eliminate po wer drain. 10 tmr3l internal data bus 8 set tmr3if on overflow tmr3 tmr3h high byte 8 8 8 read tmr1l write tmr1l 8 tmr3on ccp1 special event trigger timer1 oscillator on/off timer3 timer1 clock input ccp1 select from t3con<3> clear tmr3 t1oscen (1) intosc without clkout downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 105 pic18(l)f1xk22 12.2 timer3 16-bit read/write mode timer3 can be configured for 16-bit reads and writes (see figure 12-2 ). when the rd16 control bit of the t3con register is set, the address for tmr3h is mapped to a buffer register for the high byte of timer3. a read from tmr3l will load the contents of the high byte of timer3 into the timer3 high byte buffer register. this provides the user with the ability to accurately read all 16 bits of timer1 without having to determine whether a read of the high byte, followed by a read of the low byte, has become invalid due to a rollover between reads. a write to the high byte of timer3 must also take place through the tmr3h buffer register. the timer3 high byte is updated with the contents of tmr3h when a write occurs to tmr3l. this allows a user to write all 16 bits to both the high and low bytes of timer3 at once. the high byte of timer3 is not directly readable or writable in this mode. all reads and writes must take place through the timer3 high byte buffer register. writes to tmr3h do not clear the timer3 prescaler. the prescaler is only cleared on writes to tmr3l. 12.3 using the timer1 oscillator as the timer3 clock source the timer1 internal oscillator may be used as the clock source for timer3. the timer1 oscillator is enabled by setting the t1oscen bit of the t1con register. to use it as the timer3 clock source, the tmr3cs bit must also be set. as previously noted, this also configures timer3 to increment on every rising edge of the oscillator source. the timer1 oscillator is described in section 10.0 ?timer1 module? . 12.4 timer3 interrupt the tmr3 register pair (tmr3h:tmr3l) increments from 0000h to ffffh and overflows to 0000h. the timer3 interrupt, if enabled, is generated on overflow and is latched in interrupt flag bit, tmr3if of the pir2 register. this interrupt can be enabled or disabled by setting or clearing the timer3 interrupt enable bit, tmr3ie of the pie2 register. 12.5 resetting timer3 using the ccp special event trigger if ccp1 module is configured to use timer3 and to generate a special event trigger in compare mode (ccp1m<3:0>), this signal will reset timer3. it will also start an a/d conversion if the a/d module is enabled (see section 16.2.8 ?special event trigger? for more information). the module must be configured as either a timer or synchronous counter to take advantage of this feature. when used this way, the ccpr1h:ccpr1l register pair effectively becomes a period register for timer3. if timer3 is running in asynchronous counter mode, the reset operation may not work. in the event that a write to timer3 coincides with a special event trigger from a ccp module, the write will take precedence. table 12-1: registers associated with timer3 as a timer/counter name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr2 oscfip c1ip c2ip eeip bclip tmr3ip 248 pie2 oscfie c1ie c2ie eeie bclie tmr3ie 248 pir2 oscfif c1if c2if eeif bclif tmr3if 248 tmr3h timer3 register, high byte 247 tmr3l timer3 register, low byte 247 trisa trisa5 trisa4 (1) trisa2 trisa1 trisa0 248 t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 246 t3con rd16 t3ckps1 t3ckps0 t3ccp1 t3sync tmr3cs tmr3on 247 legend: = unimplemented, read as 0 . shaded cells are not used by the timer3 module. note 1: unimplemented, read as 1 . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 106 ? 2009-2016 microchip technology inc. 13.0 enhanced capture/compare/pwm (eccp) module pic18(l)f1xk22 devices have one eccp (capture/compare/pwm) module. the module contains a 16-bit register which can operate as a 16-bit capture register, a 16-bit compare register or a pwm master/slave duty cycle register. ccp1 is implemented as a standard ccp module with enhanced pwm capabilities. these include: provision for two or four output channels output steering programmable polarity programmable dead-band control automatic shutdown and restart the enhanced features are discussed in detail in section 13.4 ?pwm (enhanced mode)? . register 13-1: ccp1con: enhanced capt ure/compare/pwm control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 p1m1 p1m0 dc1b1 dc1b0 ccp1m3 ccp1m2 ccp1m1 ccp1m0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-6 p1m<1:0>: enhanced pwm output configuration bits if ccp1m<3:2> = 00 , 01 , 10 : xx = p1a assigned as capture/compare input/output; p1b, p1c, p1d assigned as port pins if ccp1m<3:2> = 11 : 00 = single output: p1a, p1b, p1c and p1d controlled by steering (see section 13.4.7 ?pulse steering mode? ). 01 = full-bridge output forward: p1d modulated; p1a active; p1b, p1c inactive 10 = half-bridge output: p1a, p1b modulated with dead-band control; p1c, p1d assigned as port pins 11 = full-bridge output reverse: p1b modulated; p1c active; p1a, p1d inactiv e bit 5-4 dc1b<1:0> : pwm duty cycle bit 1 and bit 0 capture mode: unused. compare mode: unused. pwm mode: these bits are the two lsbs of the 10-bit pwm duty cycle. the eight msbs of the duty cycle are fo und in ccpr1l. bit 3-0 ccp1m<3:0> : enhanced ccp mode select bits 0000 = capture/compare/pwm off (resets eccp module) 0001 = reserved 0010 = compare mode, toggle output on match 0011 = reserved 0100 = capture mode, every falling edge 0101 = capture mode, every rising edge 0110 = capture mode, every 4th rising edge 0111 = capture mode, every 16th rising edge 1000 = compare mode, initialize ccp1 pin low, set output on compare match ( set ccp1if) 1001 = compare mode, initialize ccp1 pin high, clear output on compare match (set ccp1if ) 1010 = compare mode, generate software interrupt only, ccp1 pin reverts to i/o state 1011 = compare mode, trigger special event (eccp resets tmr1 or tmr3, start a/d conversion, sets cc1if bit) 1100 = pwm mode; p1a, p1c active-high; p1b, p1d active-high 1101 = pwm mode; p1a, p1c active-high; p1b, p1d active-low 1110 = pwm mode; p1a, p1c active-low; p1b, p1d active-high 1111 = pwm mode; p1a, p1c active-low; p1b, p1d active-low downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 107 pic18(l)f1xk22 in addition to the expanded range of modes available through the ccp1con register and eccp1as register, the eccp module has two additional registers associated with enhanced pwm operation and auto-shutdown features. they are: pwm1con (dead-band delay) pstrcon (output steering) 13.1 eccp outputs and configuration the enhanced ccp module may have up to four pwm outputs, depending on the selected operating mode. these outputs, designated p1a through p1d, are multiplexed with i/o pins on portc. the outputs that are active depend on the ccp operating mode selected. the pin assignments are summarized in table 13-2 . to configure the i/o pins as pwm outputs, the proper pwm mode must be selected by setting the p1m<1:0> and ccp1m<3:0> bits. the appropriate trisc direction bits for the port pins must also be set as outputs. 13.1.1 ccp module and timer resources the ccp modules utilize timers 1, 2 or 3, depending on the mode selected. timer1 and timer3 are available to modules in capture or compare modes, while timer2 is available for modules in pwm mode. table 13-1: ccp mode ? timer resource the assignment of a particular timer to a module is determined by the timer-to-ccp enable bits in the t3con register ( register 12-1 ). the interactions between the two modules are summarized in figure 13-1 . in asynchronous counter mode, the capture operation will not work reliably. 13.2 capture mode in capture mode, the ccpr1h:ccpr1l register pair captures the 16-bit value of the tmr1 or tmr3 registers when an event occurs on the corresponding ccp1 pin. an event is defined as one of the following: every falling edge every rising edge every 4th rising edge every 16th rising edge the event is selected by the mode select bits, ccp1m<3:0> of the ccp1con register. when a capture is made, the interrupt request flag bit, ccp1if, is set; it must be cleared by software. if another capture occurs before the value in register ccpr1 is read, the old captured value is overwritten by the new captured value. 13.2.1 ccp pin configuration in capture mode, the appropriate ccp1 pin should be configured as an input by setting the corresponding tris direction bit. 13.2.2 timer1/timer3 mode selection the timers that are to be used with the capture feature (timer1 and/or timer3) must be running in timer mode or synchronized counter mode. in asynchronous counter mode, the capture operation may not work. the timer to be used with each ccp module is selected in the t3con register (see section 13.1.1 ?ccp module and timer resources? ). 13.2.3 software interrupt when the capture mode is changed, a false capture interrupt may be generated. the user should keep the ccp1ie interrupt enable bit clear to avoid false interrupts. the interrupt flag bit, ccp1if, should also be cleared following any such change in operating mode. ccp/eccp mode timer resource capture timer1 or timer3 compare timer1 or timer3 pwm timer2 note: if the ccp1 pin is configured as an output, a write to the port can cause a capture condition. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 108 ? 2009-2016 microchip technology inc. 13.2.4 ccp prescaler there are four prescaler settings in capture mode; they are specified as part of the operating mode selected by the mode select bits (ccp1m<3:0>). whenever the ccp module is turned off or capture mode is disabled, the prescaler counter is cleared. this means that any reset will clear the prescaler counter. switching from one capture prescaler to another may generate an interrupt. also, the prescaler counter will not be cleared; therefore, the first capture may be from a non-zero prescaler. example 13-1 shows the recommended method for switching between capture prescalers. this example also clears the prescaler counter and will not generate the false interrupt. example 13-1: changing between capture prescalers figure 13-1: capture mode operat ion block diagram clrf ccp1con ; turn ccp module off movlw new_capt_ps ; load wreg with the ; new prescaler mode; value and ccp on movwf ccp1con ; load ccp1con with ; this value ccpr1h ccpr1l tmr1h tmr1l set ccp1if tmr3 enable q1:q4 ccp1con<3:0> ccp1 pin prescaler ? 1, 4, 16 and edge detect tmr1 enable t3ccp1 t3ccp1 tmr3h tmr3l 4 4 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 109 pic18(l)f1xk22 13.3 compare mode in compare mode, the 16-bit ccpr1 register value is constantly compared against either the tmr1 or tmr3 register pair value. when a match occurs, the ccp1 pin can be: driven high driven low toggled (high-to-low or low-to-high) remain unchanged (that is, reflects the state of the i/o latch) the action on the pin is based on the value of the mode select bits (ccp1m<3:0>). at the same time, the interrupt flag bit, ccp1if, is set. 13.3.1 ccp pin configuration the user must configure the ccp1 pin as an output by clearing the appropriate tris bit. 13.3.2 timer1/timer3 mode selection timer1 and/or timer3 must be running in timer mode or synchronized counter mode if the ccp module is using the compare feature. in asynchronous counter mode, the compare operation will not work reliably. 13.3.3 software interrupt mode when the generate software interrupt mode is chosen (ccp1m<3:0> = 1010 ), the ccp1 pin is not affected. only the ccp1if interrupt flag is affected. 13.3.4 special event trigger the ccp module is equipped with a special event trigger. this is an internal hardware signal generated in compare mode to trigger actions by other modules. the special event trigger is enabled by selecting the compare special event trigger mode (ccp1m<3:0> = 1011 ). the special event trigger resets the timer register pair for whichever timer resource is currently assigned as the modules time base. this allows the ccpr1 registers to serve as a programmable period register for either timer. the special event trigger can also start an a/d conversion. in order to do this, the a/d converter must already be enabled. figure 13-2: compare mode operat ion block diagram note: clearing the ccp1con register will force the ccp1 compare output latch (depending on device configuration) to the default low level. this is not the portc i/o data latch. tmr1h tmr1l tmr3h tmr3l ccpr1h ccpr1l comparator t3ccp1 set ccp1if 1 0 q s r output logic special event trigger ccp1 pin tris ccp1con<3:0> output enable 4 (timer1/timer3 reset, a/d trigger) compare match downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 110 ? 2009-2016 microchip technology inc. 13.4 pwm (enhanced mode) the enhanced pwm mode can generate a pwm signal on up to four different output pins with up to 10-bits of resolution. it can do this through four different pwm output modes: single pwm half-bridge pwm full-bridge pwm, forward mode full-bridge pwm, reverse mode to select an enhanced pwm mode, the p1m bits of the ccp1con register must be set appropriately. the pwm outputs are multiplexed with i/o pins and are designated p1a, p1b, p1c and p1d. the polarity of the pwm pins is configurable and is selected by setting the ccp1m bits in the ccp1con register appropriately. table 13-1 shows the pin assignments for each enhanced pwm mode. figure 13-3 shows an example of a simplified block diagram of the enhanced pwm module. figure 13-3: example simplified block diagram of the enhanced pwm mode table 13-2: example pin assignments for various pwm enhanced modes note: to prevent the generation of an incomplete waveform when the pwm is first enabled, the eccp module waits until the start of a new pwm period before generating a pwm signal. ccpr1l ccpr1h (slave) comparator tmr2 comparator pr2 (1) rq s duty cycle registers dc1b<1:0> clear timer2, toggle pwm pin and latch duty cycle note 1: the 8-bit timer tmr2 register is concatenated with the 2-bit internal q clock, or 2 bits of the prescal er to create the 10-bit time base. tris ccp1/p1a tris p1b tris p1c tris p1d output controller p1m<1:0> 2 ccp1m<3:0> 4 pwm1con ccp1/p1a p1b p1c p1d note 1: the tris register value for each pwm output must be configured appropriately. 2: any pin not used by an enhanced pwm mode is available for alternate pin functions. eccp mode p1m<1:0> ccp1/p1a p1b p1c p1d single 00 yes (1) yes (1) yes (1) yes (1) half-bridge 10 yes yes no no full-bridge, forward 01 yes yes yes yes full-bridge, reverse 11 yes yes yes yes note 1: outputs are enabled by pulse steering in single mode. see register 13-4 . downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 111 pic18(l)f1xk22 figure 13-4: example pwm (enhanced mode) output relationships (active-high state) 0 period 0010 01 11 signal pr2+1 p1m<1:0> p1a modulated p1a modulated p1b modulated p1a active p1b inactive p1c inactive p1d modulated p1a inactive p1b modulated p1c active p1d inactive pulse width (single output) (half-bridge) (full-bridge, forward) (full-bridge, reverse) delay (1) delay (1) relationships: period = 4 * t osc * (pr2 + 1) * (tmr2 prescale value) pulse width = t osc * (ccpr1l<7:0>:ccp1con<5:4>) * (tmr2 prescale value) delay = 4 * t osc * (pwm1con<6:0>) note 1: dead-band delay is programmed using the pwm1con register ( section 13.4.6 ?programmable dead-band delay mode? ). downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 112 ? 2009-2016 microchip technology inc. figure 13-5: example enhanced pwm outp ut relationships (active-low state) 0 period 0010 01 11 signal pr2+1 p1m<1:0> p1a modulated p1a modulated p1b modulated p1a active p1b inactive p1c inactive p1d modulated p1a inactive p1b modulated p1c active p1d inactive pulse width (single output) (half-bridge) (full-bridge, forward) (full-bridge, reverse) delay (1) delay (1) relationships: period = 4 * t osc * (pr2 + 1) * (tmr2 prescale value) pulse width = t osc * (ccpr1l<7:0>:ccp1con<5:4>) * (tmr2 prescale value) delay = 4 * t osc * (pwm1con<6:0>) note 1: dead-band delay is programmed using the pwm1con register ( section 13.4.6 ?programmable dead-band delay mode? ). downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 113 pic18(l)f1xk22 13.4.1 half-bridge mode in half-bridge mode, two pins are used as outputs to drive push-pull loads. the pwm output signal is output on the ccp1/p1a pin, while the complementary pwm output signal is output on the p1b pin (see figure 13-6 ). this mode can be used for half-bridge applications, as shown in figure 13-7 , or for full-bridge applications, where four power switches are being modulated with two pwm signals. in half-bridge mode, the programmable dead-band delay can be used to prevent shoot-through current in half-bridge power devices. the value of the pdc<6:0> bits of the pwm1con register sets the number of instruction cycles before the output is driven active. if the value is greater than the duty cycle, the corresponding output remains inactive during the entire cycle. see section 13.4.6 ?programmable dead-band delay mode? for more details of the dead-band delay operations. since the p1a and p1b outputs are multiplexed with the port data latches, the associated tris bits must be cleared to configure p1a and p1b as outputs. figure 13-6: example of half-bridge pwm output figure 13-7: example of half-bridge applications period pulse width td td (1) p1a (2) p1b (2) td = dead-band delay period (1) (1) note 1: at this time, the tmr2 register is equal to the pr2 register. 2: output signals are shown as active-high. p1a p1b fet driver fet driver load + - + - fet driver fet driver v+ load fet driver fet driver p1ap1b standard half-bridge circuit (?push-pull?) half-bridge output driving a full-bridge circuit downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 114 ? 2009-2016 microchip technology inc. 13.4.2 full-bridge mode in full-bridge mode, all four pins are used as outputs. an example of full-bridge application is shown in figure 13-8 . in the forward mode, pin ccp1/p1a is driven to its active state, pin p1d is modulated, while p1b and p1c will be driven to their inactive state as shown in figure 13-9 . in the reverse mode, p1c is driven to its active state, pin p1b is modulated, while p1a and p1d will be driven to their inactive state as shown figure 13-9 . p1a, p1b, p1c and p1d outputs are multiplexed with the port data latches. the associated tris bits must be cleared to configure the p1a, p1b, p1c and p1d pins as outputs. figure 13-8: example of full-bridge application p1ap1c fet driver fet driver v+ v- load fet driver fet driver p1bp1d qaqb qd qc downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 115 pic18(l)f1xk22 figure 13-9: example of full-bridge pwm output period pulse width p1a (2) p1b (2) p1c (2) p1d (2) forward mode (1) period pulse width p1a (2) p1c (2) p1d (2) p1b (2) reverse mode (1) (1) (1) note 1: at this time, the tmr2 register is equal to the pr2 register. 2: output signal is shown as active-high. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 116 ? 2009-2016 microchip technology inc. 13.4.2.1 direction change in full-bridge mode in the full-bridge mode, the p1m1 bit in the ccp1con register allows users to control the forward/reverse direction. when the application firmware changes this direction control bit, the module will change to the new direction on the next pwm cycle. a direction change is initiated in software by changing the p1m1 bit of the ccp1con register. the following sequence occurs prior to the end of the current pwm period: the modulated outputs (p1b and p1d) are placed in their inactive state. the associated unmodulated outputs (p1a and p1c) are switched to drive in the opposite direction. pwm modulation resumes at the beginning of the next period. see figure 13-10 for an illustration of this sequence. the full-bridge mode does not provide dead-band delay. as one output is modulated at a time, dead-band delay is generally not required. there is a situation where dead-band delay is required. this situation occurs when both of the following conditions are true: 1. the direction of the pwm output changes when the duty cycle of the output is at or near 100%. 2. the turn off time of the power switch, including the power device and driver circuit, is greater than the turn on time. figure 13-11 shows an example of the pwm direction changing from forward to reverse, at a near 100% duty cycle. in this example, at time t1, the output p1a and p1d become inactive, while output p1c becomes active. since the turn off time of the power devices is longer than the turn on time, a shoot-through current will flow through power devices qc and qd (see figure 13-8 ) for the duration of t. the same phenomenon will occur to power devices qa and qb for pwm direction change from reverse to forward. if changing pwm direction at high duty cycle is required for an application, two possible solutions for eliminating the shoot-through current are: 1. reduce pwm duty cycle for one pwm period before changing directions. 2. use switch drivers that can drive the switches off faster than they can drive them on. other options to prevent shoot-through current may exist. figure 13-10: example of pwm direction change pulse width period (1) signal note 1: the direction bit p1m1 of the ccp1con register is written any time during the pwm cycle. 2: when changing directions, the p1a and p1c signals switch before the end of the current pwm cycle. the modulated p1b and p1d signals are inactive at this time. the length of this time is (1/f osc ) ? tmr2 prescale value. period (2) p1a (active-high) p1b (active-high) p1c (active-high) p1d (active-high) pulse width downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 117 pic18(l)f1xk22 figure 13-11: example of pwm direct ion change at near 100% duty cycle 13.4.3 start-up considerations when any pwm mode is used, the application hardware must use the proper external pull-up and/or pull-down resistors on the pwm output pins. the ccp1m<1:0> bits of the ccp1con register allow the user to choose whether the pwm output signals are active-high or active-low for each pair of pwm output pins (p1a/p1c and p1b/p1d). the pwm output polarities must be selected before the pwm pin output drivers are enabled. changing the polarity configuration while the pwm pin output drivers are enable is not recommended since it may result in damage to the application circuits. the p1a, p1b, p1c and p1d output latches may not be in the proper states when the pwm module is initialized. enabling the pwm pin output drivers at the same time as the enhanced pwm modes may cause damage to the application circuit. the enhanced pwm modes must be enabled in the proper output mode and complete a full pwm cycle before enabling the pwm pin output drivers. the completion of a full pwm cycle is indicated by the tmr2if bit of the pir1 register being set as the second pwm period begins. forward period reverse period p1a t on t off t = t off C t on p1b p1cp1d external switch d potential shoot-through current note 1: all signals are shown as active-high. 2: t on is the turn on delay of power switch qc and its driver. 3: t off is the turn off delay of pow er switch qd and its driver. external switch c t1 pw pw note: when the microcontroller is released from reset, all of the i/o pins are in the high-impedance state. the external circuits must keep the power switch devices in the off state until the microcontroller drives the i/o pins with the proper signal levels or activates the pwm output(s). downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 118 ? 2009-2016 microchip technology inc. 13.4.4 enhanced pwm auto-shutdown mode the pwm mode supports an auto-shutdown mode that will disable the pwm outputs when an external shutdown event occurs. auto-shutdown mode places the pwm output pins into a predetermined state. this mode is used to help prevent the pwm from damaging the application. the auto-shutdown sources are selected using the eccpas<2:0> bits of the eccpas register. a shutdown event may be generated by: a logic 0 on the int0 pin a logic 1 on a comparator (cx) output a shutdown condition is indicated by the eccpase (auto-shutdown event status) bit of the eccpas register. if the bit is a 0 , the pwm pins are operating normally. if the bit is a 1 , the pwm outputs are in the shutdown state. when a shutdown event occurs, two things happen: the eccpase bit is set to 1 . the eccpase will remain set until cleared in firmware or an auto-restart occurs (see section 13.4.5 ?auto-restart mode? ). the enabled pwm pins are asynchronously placed in their shutdown states. the pwm output pins are grouped into pairs [p1a/p1c] and [p1b/p1d]. the state of each pin pair is determined by the pssac and pssbd bits of the eccpas register. each pin pair may be placed into one of three states: drive logic 1 drive logic 0 tri-state (high-impedance) register 13-2: eccp1as: enhanced cap ture/compare/pwm auto-shutdown control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 eccpase eccpas2 eccpas1 eccpas0 pssac1 pssac0 pssbd1 pssbd0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 eccpase: eccp auto-shutdown event status bit 1 = a shutdown event has occurred; eccp outputs are in shutdown state 0 = eccp outputs are operating bit 6-4 eccpas<2:0>: eccp auto-shutdown source select bits 000 = auto-shutdown is disabled 001 = comparator c1out output is high 010 = comparator c2out output is high 011 = either comparator c1out or c2out is high 100 =v il on int0 pin 101 =v il on int0 pin or comparator c1out output is high 110 =v il on int0 pin or comparator c2out output is high 111 =v il on int0 pin or comparator c1out or comparator c2out is high bit 3-2 pssacn: pins p1a and p1c shutdown state control bits 00 = drive pins p1a and p1c to 0 01 = drive pins p1a and p1c to 1 10 = pins 1a and p1c tri-state 11 = reserved, do not use bit 1-0 pssbdn: pins p1b and p1d shutdown state control bits 00 = drive pins p1b and p1d to 0 01 = drive pins p1b and p1d to 1 10 = pins p1b and p1d tri-state 11 = reserved, do not use downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 119 pic18(l)f1xk22 figure 13-12: pwm auto-shutdown wi th firmware restart (prsen = 0 ) note 1: the auto-shutdown condition is a level-based signal, not an edge-based signal. as long as the level is present, the auto-shutdown will persist. 2: writing to the eccpase bit is disabled while an auto-shutdown condition persists. 3: once the auto-shutdown condition has been removed and the pwm restarted (either through firmware or auto-restart) the pwm signal will always restart at the beginning of the next pwm period. 4: prior to an auto-shutdown event caused by a comparator output or int pin event, a software shutdown can be triggered in firmware by setting the ccpxase bit to a 1 . the auto-restart feature tracks the active status of a shutdown caused by a comparator output or int pin event only, so if it is enabled at this time, it will immediately clear this bit and restart the eccp module at the beginning of the next pwm period. shutdown pwm eccpase bit activity event shutdown event occurs shutdown event clears pwm resumes normal pwm start of pwm period eccpase cleared by firmware pwm period downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 120 ? 2009-2016 microchip technology inc. 13.4.5 auto-restart mode the enhanced pwm can be configured to automatically restart the pwm signal once the auto-shutdown condition has been removed. auto-restart is enabled by setting the prsen bit in the pwm1con register. if auto-restart is enabled, the eccpase bit will remain set as long as the auto-shutdown condition is active. when the auto-shutdown condition is removed, the eccpase bit will be cleared via hardware and normal operation will resume. figure 13-13: pwm auto-shutdown wi th auto-restart enabled (prsen = 1 ) shutdown pwm eccpase bit activity event shutdown event occurs shutdown event clears pwm resumes normal pwm start of pwm period pwm period downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 121 pic18(l)f1xk22 13.4.6 programmable dead-band delay mode in half-bridge applications where all power switches are modulated at the pwm frequency, the power switches normally require more time to turn off than to turn on. if both the upper and lower power switches are switched at the same time (one turned on, and the other turned off), both switches may be on for a short period of time until one switch completely turns off. during this brief interval, a very high current (shoot-through current) will flow through both power switches, shorting the bridge supply. to avoid this potentially destructive shoot-through current from flowing during switching, turning on either of the power switches is normally delayed to allow the other switch to completely turn off. in half-bridge mode, a digitally programmable dead-band delay is available to avoid shoot-through current from destroying the bridge power switches. the delay occurs at the signal transition from the non-active state to the active state. see figure 13-14 for illustration. the lower seven bits of the associated pwm1con register ( register 13-3 ) sets the delay period in terms of microcontroller instruction cycles (t cy or 4 t osc ). figure 13-14: example of half-bridge pwm output figure 13-15: example of half-bridge applications period pulse width td td (1) p1a (2) p1b (2) td = dead-band delay period (1) (1) note 1: at this time, the tmr2 register is equal to the pr2 register. 2: output signals are shown as active-high. p1a p1b fet driver fet driver v+ v- load +v - +v - standard half-bridge circuit (?push-pull?) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 122 ? 2009-2016 microchip technology inc. register 13-3: pwm1con: enhanced pwm control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 prsen pdc6 pdc5 pdc4 pdc3 pdc2 pdc1 pdc0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 prsen: pwm restart enable bit 1 = upon auto-shutdown, the eccpase bit clears automatically once the shutdown event goes away; the pwm restarts automatically 0 = upon auto-shutdown, eccpase must be cleared by software to restart the pwm bit 6-0 pdc<6:0>: pwm delay count bits pdcn = number of f osc /4 (4 * t osc ) cycles between the scheduled time when a pwm signal should transition active and the actual time it transitions active downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 123 pic18(l)f1xk22 13.4.7 pulse steering mode in single output mode, pulse steering allows any of the pwm pins to be the modulated signal. additionally, the same pwm signal can be simultaneously available on multiple pins. once the single output mode is selected (ccp1m<3:2> = 11 and p1m<1:0> = 00 of the ccp1con register), the user firmware can bring out the same pwm signal to one, two, three or four output pins by setting the appropriate str bits of the pstrcon register, as shown in tab l e 1 3- 2 . while the pwm steering mode is active, ccp1m<1:0> bits of the ccp1con register select the pwm output polarity for the p1 pins. the pwm auto-shutdown operation also applies to pwm steering mode as described in section 13.4.4 ?enhanced pwm auto-shutdown mode? . an auto-shutdown event will only affect pins that have pwm outputs enabled. note: the associated tris bits must be set to output ( 0 ) to enable the pin output driver in order to see the pwm signal on the pin. register 13-4: pstrcon: pul se steering control register (1) u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-1 strsync strd strc strb stra bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-5 unimplemented: read as 0 bit 4 strsync: steering sync bit 1 = output steering update occurs on next pwm period 0 = output steering update occurs at the beginning of the instruction cycle boundary bit 3 strd: steering enable bit d 1 = p1d pin has the pwm waveform with polarity control from ccp1m<1:0> 0 = p1d pin is assigned to port pin bit 2 strc: steering enable bit c 1 = p1c pin has the pwm waveform with polarity control from ccp1m<1:0> 0 = p1c pin is assigned to port pin bit 1 strb: steering enable bit b 1 = p1b pin has the pwm waveform with polarity control from ccp1m<1:0> 0 = p1b pin is assigned to port pin bit 0 stra: steering enable bit a 1 = p1a pin has the pwm waveform with polarity control from ccp1m<1:0> 0 = p1a pin is assigned to port pin note 1: the pwm steering mode is available only when the ccp1con register bits ccp1m<3 :2> = 11 and p1m<1:0> = 00 . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 124 ? 2009-2016 microchip technology inc. figure 13-16: simplified steering block diagram 10 tris p1a pin port data p1a signal stra 10 tris p1b pin port data strb 10 tris p1c pin port data strc 10 tris p1d pin port data strd note 1: port outputs are configured as shown when the ccp1con register bits p1m<1:0> = 00 and ccp1m<3:2> = 11. 2: single pwm output requires setting at least one of the strx bits. ccp1m1 ccp1m0 ccp1m1 ccp1m0 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 125 pic18(l)f1xk22 13.4.7.1 steering synchronization the strsync bit of the pstrcon register gives the user two selections of when the steering event will happen. when the strsync bit is 0 , the steering event will happen at the end of the instruction that writes to the pstrcon register. in this case, the output signal at the p1 pins may be an incomplete pwm waveform. this operation is useful when the user firmware needs to immediately remove a pwm signal from the pin. when the strsync bit is 1 , the effective steering update will happen at the beginning of the next pwm period. in this case, steering on/off the pwm output will always produce a complete pwm waveform. figures 13-17 and 13-18 illustrate the timing diagrams of the pwm steering depending on the strsync setting. figure 13-17: example of steering ev ent at end of instruction (strsync = 0) figure 13-18: example of steering event at beginning of instruction (strsync = 1) pwm p1n = pwm strn p1 port data pwm period port data pwm port data p1n = pwm strn p1 port data downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 126 ? 2009-2016 microchip technology inc. 13.4.8 operation in power-managed modes in sleep mode, all clock sources are disabled. timer2 will not increment and the state of the module will not change. if the eccp pin is driving a value, it will continue to drive that value. when the device wakes up, it will continue from this state. if two-speed start-ups are enabled, the initial start-up frequency from hfintosc and the postscaler may not be stable immediately. in pri_idle mode, the primary clock will continue to clock the eccp module without change. in all other power-managed modes, the selected power-managed mode clock will clock timer2. other power-managed mode clocks will most likely be different than the primary clock frequency. 13.4.8.1 operation with fail-safe clock monitor if the fail-safe clock monitor is enabled, a clock failure will force the device into the rc_run power-managed mode and the oscfif bit of the pir2 register will be set. the eccp will then be clocked from the internal oscillator clock source, which may have a different clock frequency than the primary clock. see the previous section for additional details. 13.4.9 effects of a reset both power-on reset and subsequent resets will force all ports to input mode and the ccp registers to their reset states. this forces the enhanced ccp module to reset to a state compatible with the standard ccp module. table 13-3: registers associated with eccp1 module and timer1 to timer3 name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page ccpr1h capture/compare/pwm register 1, high byte 247 ccpr1l capture/compare/pwm register 1, low byte 247 ccp1con p1m1 p1m0 dc1b1 dc1b0 ccp1m3 ccp1m2 ccp1m1 ccp1m0 247 eccp1as eccpase eccpas2 eccpas1 eccpas0 pssac1 pssac0 pssbd1 pssbd0 247 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr1 adip rcip txip sspip ccp1ip tmr2ip tmr1ip 248 ipr2 oscfip c1ip c2ip eeip bclip tmr3ip 248 pie1 adie rcie txie sspie ccp1ie tmr2ie tmr1ie 248 pie2 oscfie c1ie c2ie eeie bclie tmr3ie 248 pir1 adif rcif txif sspif ccp1if tmr2if tmr1if 248 pir2 oscfif c1if c2if eeif bclif tmr3if 248 pr2 timer2 period register 246 pwm1con prsen pdc6 pdc5 pdc4 pdc3 pdc2 pdc1 pdc0 247 rcon ipen sboren ri to pd por bor 246 tmr1h timer1 register, high byte 246 tmr1l timer1 register, low byte 246 tmr2 timer2 register 246 tmr3h timer3 register, high byte 247 tmr3l timer3 register, low byte 247 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 248 t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 246 t2con t2outps3 t2outps2 t2outps1 t2outps0 tmr2on t2ckps1 t2ckps0 246 t3con rd16 t3ckps1 t3ckps0 t3ccp1 t3sync tmr3cs tmr3on 247 legend: = unimplemented, read as 0 . shaded cells are not used during eccp operation. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 127 pic18(l)f1xk22 14.0 master synchronous serial port (mssp) module 14.1 master ssp (mssp) module overview the master synchronous serial port (mssp) module is a serial interface, useful for communicating with other peripheral or microcontroller devices. these peripheral devices may be serial eeproms, shift registers, display drivers, a/d converters, etc. the mssp module can operate in one of two modes: serial peripheral interface (spi) inter-integrated circuit (i 2 c) - full master mode - slave mode (with general address call) the i 2 c interface supports the following modes in hardware: master mode multi-master mode slave mode 14.2 spi mode the spi mode allows eight bits of data to be synchronously transmitted and received simultaneously. all four modes of spi are supported. to accomplish communication, typically three pins are used: serial data out C sdo serial data in C sdi serial clock C sck additionally, a fourth pin may be used when in a slave mode of operation: slave select C ss figure 14-1 shows the block diagram of the mssp module when operating in spi mode. figure 14-1: mssp block diagram (spi mode) ( ) read write internal data bus sspsr reg sspm<3:0> bit 0 shift clock ss control enable edge select clock select tmr2 output t osc prescaler 4, 16, 64 2 edge select 2 4 tris bit sdo sspbuf reg sdi/sda ss sck/scl downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 128 ? 2009-2016 microchip technology inc. 14.2.1 registers the mssp module has four registers for spi mode operation. these are: sspcon1 C control register sspstat C status register sspbuf C serial receive/transmit buffer sspsr C shift register (not directly accessible) sspcon1 and sspstat are the control and status registers in spi mode operation. the sspcon1 register is readable and writable. the lower six bits of the sspstat are read-only. the upper two bits of the sspstat are read/write. sspsr is the shift register used for shifting data in and out. sspbuf provides indirect access to the sspsr register. sspbuf is the buffer register to which data bytes are written, and from which data bytes are read. in receive operations, sspsr and sspbuf together create a double-buffered receiver. when sspsr receives a complete byte, it is transferred to sspbuf and the sspif interrupt is set. during transmission, the sspbuf is not double-buffered. a write to sspbuf will write to both sspbuf and sspsr. register 14-1: sspstat: mssp status register (spi mode) r/w-0 r/w-0 r-0 r-0 r-0 r-0 r-0 r-0 smp cke d/a psr / w ua bf bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 smp: sample bit spi master mode: 1 = input data sampled at end of data output time 0 = input data sampled at middle of data output time spi slave mode: smp must be cleared when spi is used in slave mode. bit 6 cke: spi clock select bit (1) 1 = transmit occurs on transition from active to idle clock state 0 = transmit occurs on transition from idle to active clock state bit 5 d/a : data/address bit used in i 2 c mode only. bit 4 p: stop bit used in i 2 c mode only. this bit is cleared when the mssp module is disabled, sspen is cleared. bit 3 s: start bit used in i 2 c mode only. bit 2 r/w : read/write information bit used in i 2 c mode only. bit 1 ua: update address bit used in i 2 c mode only. bit 0 bf: buffer full status bit (receive mode only) 1 = receive complete, sspbuf is full 0 = receive not complete, sspbuf is empty note 1: polarity of clock state is set by the ckp bit of the sspcon1 register. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 129 pic18(l)f1xk22 register 14-2: sspcon1: mssp co ntrol 1 register (spi mode) r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 wcol: write collision detect bit (transmit mode only) 1 = the sspbuf register is written while it is still transmitting the previous word (must be cleared by software) 0 = no collision bit 6 sspov: receive overflow indicator bit (1) spi slave mode: 1 = a new byte is received while the sspbuf register is still holding the previous data. in case of overflow, the data in sspsr is lost. overflow can only occur in slave mode. the user must read the sspbuf, even if only transmitting data, to avoid setting overflow (must be cleared by software). 0 = no overflow bit 5 sspen: synchronous serial port enable bit (2) 1 = enables serial port and configures sck, sdo, sdi and ss as serial port pins 0 = disables serial port and configures these pins as i/o port pins bit 4 ckp: clock polarity select bit 1 = idle state for clock is a high level 0 = idle state for clock is a low level bit 3-0 sspm<3:0>: synchronous serial port mode select bits (3) 0101 = spi slave mode, clock = sck pin, ss pin control disabled, ss can be used as i/o pin 0100 = spi slave mode, clock = sck pin, ss pin control enabled 0011 = spi master mode, clock = tmr2 output/2 0010 = spi master mode, clock = f osc /64 0001 = spi master mode, clock = f osc /16 0000 = spi master mode, clock = f osc /4 note 1: in master mode, the overflow bit is not set since each new reception (and transmission) is initiated by writing to the sspbuf register. 2: when enabled, these pins must be properly configured as input or output. 3: bit combinations not specifically listed here are either reserved or implemented in i 2 c mode only. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 130 ? 2009-2016 microchip technology inc. 14.2.2 operation when initializing the spi, several options need to be specified. this is done by programming the appropriate control bits (sspcon1<5:0> and sspstat<7:6>). these control bits allow the following to be specified: master mode (sck is the clock output) slave mode (sck is the clock input) clock polarity (idle state of sck) data input sample phase (middle or end of data output time) clock edge (output data on rising/falling edge of sck) clock rate (master mode only) slave select mode (slave mode only) the mssp consists of a transmit/receive shift register (sspsr) and a buffer register (sspbuf). the sspsr shifts the data in and out of the device, msb first. the sspbuf holds the data that was written to the sspsr until the received data is ready. once the 8 bits of data have been received, that byte is moved to the sspbuf register. then, the buffer full detect bit, bf of the sspstat register, and the interrupt flag bit, sspif, are set. this double-buffering of the received data (sspbuf) allows the next byte to start reception before reading the data that was just received. any write to the sspbuf register during transmission/reception of data will be ignored and the write collision detect bit wcol of the sspcon1 register, will be set. user software must clear the wcol bit to allow the following write(s) to the sspbuf register to complete successfully. when the application software is expecting to receive valid data, the sspbuf should be read before the next byte of data to transfer is written to the sspbuf. the buffer full bit, bf of the sspstat register, indicates when sspbuf has been loaded with the received data (transmission is complete). when the sspbuf is read, the bf bit is cleared. this data may be irrelevant if the spi is only a transmitter. generally, the mssp interrupt is used to determine when the transmission/reception has completed. if the interrupt method is not going to be used, then software polling can be done to ensure that a write collision does not occur. example 14-1 shows the loading of the sspbuf (sspsr) for data transmission. the sspsr is not directly readable or writable and can only be accessed by addressing the sspbuf register. additionally, the mssp status register (sspstat) indicates the various status conditions. example 14-1: loading the sspbuf (sspsr) register loop btfss sspstat, bf ;has data been received (transmit complete)? bra loop ;no movf sspbuf, w ;wreg reg = contents of sspbuf movwf rxdata ;save in user ram, if data is meaningful movf txdata, w ;w reg = contents of txdata movwf sspbuf ;new data to xmit downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 131 pic18(l)f1xk22 14.2.3 enabling spi i/o to enable the serial port, ssp enable bit, sspen of the sspcon1 register, must be set. to reset or reconfigure spi mode, clear the sspen bit, reinitialize the sspcon registers and then set the sspen bit. this configures the sdi, sdo, sck and ss pins as serial port pins. for the pins to behave as the serial port function, some must have their data direction bits (in the tris register) appropriately programmed as follows: sdi is automatically controlled by the spi module sdo must have corresponding tris bit cleared sck (master mode) must have corresponding tris bit cleared sck (slave mode) must have corresponding tris bit set ss must have corresponding tris bit set any serial port function that is not desired may be overridden by programming the corresponding data direction (tris) register to the opposite value. 14.2.4 typical connection figure 14-2 shows a typical connection between two microcontrollers. the master controller (processor 1) initiates the data transfer by sending the sck signal. data is shifted out of both shift registers on their programmed clock edge and latched on the opposite edge of the clock. both processors should be programmed to the same clock polarity (ckp), then both controllers would send and receive data at the same time. whether the data is meaningful (or dummy data) depends on the application software. this leads to three scenarios for data transmission: master sends data ? C ? slave sends dummy data master sends data ? C ? slave sends data master sends dummy data ? C ? slave sends data figure 14-2: typical spi mast er/slave connection serial input buffer (sspbuf) shift register (sspsr) msb lsb sdo sdi processor 1 sck spi master sspm<3:0> = 00xx serial input buffer (sspbuf) shift register (sspsr) lsb msb sdi sdo processor 2 sck spi slave sspm<3:0> = 010x serial clock ss slave select general i/o (optional) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 132 ? 2009-2016 microchip technology inc. 14.2.5 master mode the master can initiate the data transfer at any time because it controls the sck. the master determines when the slave (processor 2, figure 14-2 ) is to broadcast data by the software protocol. in master mode, the data is transmitted/received as soon as the sspbuf register is written to. if the spi is only going to receive, the sdo output could be disabled (programmed as an input). the sspsr register will continue to shift in the signal present on the sdi pin at the programmed clock rate. as each byte is received, it will be loaded into the sspbuf register as if a normal received byte (interrupts and status bits appropriately set). the clock polarity is selected by appropriately programming the ckp bit of the sspcon1 register. this then, would give waveforms for spi communication as shown in figure 14-3 , figure 14-5 and figure 14-6 , where the msb is transmitted first. in master mode, the spi clock rate (bit rate) is user programmable to be one of the following: f osc /4 (or t cy ) f osc /16 (or 4 t cy ) f osc /64 (or 16 t cy ) timer2 output/2 this allows a maximum data rate (at 64 mhz) of 16.00 mbps. figure 14-3 shows the waveforms for master mode. when the cke bit is set, the sdo data is valid before there is a clock edge on sck. the change of the input sample is shown based on the state of the smp bit. the time when the sspbuf is loaded with the received data is shown. figure 14-3: spi mode waveform (master mode) sck (ckp = 0 sck (ckp = 1 sck (ckp = 0 sck (ckp = 1 4 clock modes input sample input sample sdi bit 7 bit 0 sdo bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 bit 7 sdi sspif (smp = 1 ) (smp = 0 ) (smp = 1 ) cke = 1 ) cke = 0 ) cke = 1 ) cke = 0 ) (smp = 0 ) write to sspbuf sspsr to sspbuf sdo bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 (cke = 0 ) (cke = 1 ) bit 0 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 133 pic18(l)f1xk22 14.2.6 slave mode in slave mode, the data is transmitted and received as external clock pulses appear on sck. when the last bit is latched, the sspif interrupt flag bit is set. before enabling the module in spi slave mode, the clock line must match the proper idle state. the clock line can be observed by reading the sck pin. the idle state is determined by the ckp bit of the sspcon1 register. while in slave mode, the external clock is supplied by the external clock source on the sck pin. this external clock must meet the minimum high and low times as specified in the electrical specifications. while in sleep mode, the slave can transmit/receive data. when a byte is received, the device will wake-up from sleep. 14.2.7 slave select synchronization the ss pin allows a synchronous slave mode. the spi must be in slave mode with ss pin control enabled (sspcon1<3:0> = 0100 ). when the ss pin is low, transmission and reception are enabled and the sdo pin is driven. when the ss pin goes high, the sdo pin is no longer driven, even if in the middle of a transmitted byte and becomes a floating output. external pull-up/pull-down resistors may be desirable depending on the application. when the spi module resets, the bit counter is forced to 0 . this can be done by either forcing the ss pin to a high level or clearing the sspen bit. figure 14-4: slave synchronization waveform note 1: when the spi is in slave mode with ss pin control enabled (sspcon<3:0> = 0100 ), the spi module will reset if the ss pin is set to v dd . 2: when the spi is used in slave mode with cke set the ss pin control must also be enabled. sck (ckp = 1 sck (ckp = 0 input sample sdi bit 7 sdo bit 7 bit 6 bit 7 sspif interrupt (smp = 0 ) cke = 0 ) cke = 0 ) (smp = 0 ) write to sspbuf sspsr to sspbuf ss flag bit 0 bit 7 bit 0 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 134 ? 2009-2016 microchip technology inc. figure 14-5: spi mode waveform (slave mode with cke = 0 ) figure 14-6: spi mode waveform (slave mode with cke = 1 ) sck (ckp = 1 sck (ckp = 0 input sample sdi bit 7 sdo bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 sspif interrupt (smp = 0 ) cke = 0 ) cke = 0 ) (smp = 0 ) write to sspbuf sspsr to sspbuf ss flag optional bit 0 sck (ckp = 1 sck (ckp = 0 input sample sdi bit 7 bit 0 sdo bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 sspif interrupt (smp = 0 ) cke = 1 ) cke = 1 ) (smp = 0 ) write to sspbuf sspsr to sspbuf ss flag not optional downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 135 pic18(l)f1xk22 14.2.8 operation in power-managed modes in spi master mode, module clocks may be operating at a different speed than when in full power mode; in the case of the sleep mode, all clocks are halted. in all idle modes, a clock is provided to the peripherals. that clock could be from the primary clock source, the secondary clock (timer1 oscillator at 32.768 khz) or the intosc source. see section 18.0 ?power-man- aged modes? for additional information. in most cases, the speed that the master clocks spi data is not important; however, this should be evaluated for each system. when mssp interrupts are enabled, after the master completes sending data, an mssp interrupt will wake the controller: from sleep, in slave mode from idle, in slave or master mode if an exit from sleep or idle mode is not desired, mssp interrupts should be disabled. in spi master mode, when the sleep mode is selected, all module clocks are halted and the transmission/reception will remain in that state until the device wakes. after the device returns to run mode, the module will resume transmitting and receiving data. in spi slave mode, the spi transmit/receive shift register operates asynchronously to the device. this allows the device to be placed in any power-managed mode and data to be shifted into the spi transmit/receive shift register. when all eight bits have been received, the mssp interrupt flag bit will be set and if enabled, will wake the device. 14.2.9 effects of a reset a reset disables the mssp module and terminates the current transfer. 14.2.10 bus mode compatibility table 14-1 shows the compatibility between the standard spi modes and the states of the ckp and cke control bits. table 14-1: spi bus modes there is also an smp bit which controls when the data is sampled. table 14-2: registers associat ed with spi operation standard spi mode terminology control bits state ckp cke 0, 0 0 1 0, 1 0 0 1, 0 1 1 1, 1 1 0 name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr1 adip rcip txip sspip ccp1ip tmr2ip tmr1ip 248 pie1 adie rcie txie sspie ccp1ie tmr2ie tmr1ie 248 pir1 adif rcif txif sspif ccp1if tmr2if tmr1if 248 trisb trisb7 trisb6 trisb5 trisb4 248 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 248 sspbuf ssp receive buffer/transmit register 246 sspcon1 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 246 sspstat smp cke d/a p s r/w ua bf 246 legend: shaded cells are not used by the mssp in spi mode. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 136 ? 2009-2016 microchip technology inc. 14.3 i 2 c mode the mssp module in i 2 c mode fully implements all master and slave functions (including general call support) and provides interrupts on start and stop bits in hardware to determine a free bus (multi-master function). the mssp module implements the standard mode specifications as well as 7-bit and 10-bit addressing. two pins are used for data transfer: serial clock C scl serial data C sda figure 14-7: mssp block diagram (i 2 c mode) 14.3.1 registers the mssp module has seven registers for i 2 c operation. these are: mssp control register 1 (sspcon1) mssp control register 2 (sspcon2) mssp status register (sspstat) serial receive/transmit buffer register (sspbuf) mssp shift register (sspsr) C not directly accessible mssp address register (sspadd) mssp address mask (sspmsk) sspcon1, sspcon2 and sspstat are the control and status registers in i 2 c mode operation. the sspcon1 and sspcon2 registers are readable and writable. the lower six bits of the sspstat are read-only. the upper two bits of the sspstat are read/write. sspsr is the shift register used for shifting data in or out. sspbuf is the buffer register to which data bytes are written to or read from. when the mssp is configured in master mode, the sspadd register acts as the baud rate generator reload value. when the mssp is configured for i 2 c slave mode the sspadd register holds the slave device address. the mssp can be configured to respond to a range of addresses by qualifying selected bits of the address register with the sspmsk register. in receive operations, sspsr and sspbuf together create a double-buffered receiver. when sspsr receives a complete byte, it is transferred to sspbuf and the sspif interrupt is set. during transmission, the sspbuf is not double-buffered. a write to sspbuf will write to both sspbuf and sspsr. note: the user must configure these pins as inputs with the corresponding tris bits. read write sspsr reg match detect sspadd reg start and stop bit detect sspbuf reg internal data bus addr match set, reset s, p bits (sspstat reg) sck/scl sdi/sda shift clock msb lsb sspmsk reg downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 137 pic18(l)f1xk22 register 14-3: sspstat: mssp status register (i 2 c mode) r/w-0 r/w-0 r-0 r-0 r-0 r-0 r-0 r-0 smp cke d/a p (1) s (1) r/w (2, 3) ua bf bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 smp: slew rate control bit in master or slave mode: 1 = slew rate control disabled for standard speed mode (100 khz and 1 mhz) 0 = slew rate control enabled for high-speed mode (400 khz) bit 6 cke: smbus select bit in master or slave mode: 1 = enable smbus specific inputs 0 = disable smbus specific inputs bit 5 d/a : data/address bit in master mode: reserved. in slave mode: 1 = indicates that the last byte received or transmitted was data 0 = indicates that the last byte received was an address bit 4 p: stop bit (1) 1 = indicates that a stop bit has been detected last 0 = stop bit was not detected last bit 3 s: start bit (1) 1 = indicates that a start bit has been detected last 0 = start bit was not detected last bit 2 r/w : read/write information bit (i 2 c mode only) (2, 3) in slave mode: 1 = read 0 = write in master mode: 1 = transmit is in progress 0 = transmit is not in progress bit 1 ua: update address bit (10-bit slave mode only) 1 = indicates that the user needs to update the address in the sspadd register 0 = address does not need to be updated bit 0 bf: buffer full status bit in transmit mode: 1 = sspbuf is full 0 = sspbuf is empty in receive mode: 1 = sspbuf is full (does not include the ack and stop bits) 0 = sspbuf is empty (does not include the ack and stop bits) note 1: this bit is cleared on reset and when sspen is cleared. 2: this bit holds the r/w bit information following the last address match. this bit is only valid from the address match to the next start bit, stop bit or not ack bit. 3: oring this bit with sen, rsen, pen, rcen or acken will indicate if the master mode is active. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 138 ? 2009-2016 microchip technology inc. register 14-4: sspcon1: ms sp control 1 register (i 2 c mode) r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 wcol: write collision detect bit in master transmit mode: 1 = a write to the sspbuf register was attempted while the i 2 c conditions were not valid for a transmission to be started (must be cleared by software) 0 = no collision in slave transmit mode: 1 = the sspbuf register is written while it is still transmitting the previous word (must be cleared by software) 0 = no collision in receive mode (master or slave modes): this is a dont care bit. bit 6 sspov: receive overflow indicator bit in receive mode: 1 = a byte is received while the sspbuf register is still holding the previous byte (must be cleared by software) 0 = no overflow in transmit mode: this is a dont care bit in transmit mode. bit 5 sspen: synchronous serial port enable bit 1 = enables the serial port and configures the sda and scl pins as the serial port pins 0 = disables serial port and configures these pins as i/o port pins when enabled, the sda and scl pins must be properly configured as inputs. bit 4 ckp: sck release control bit in slave mode: 1 = release clock 0 = holds clock low (clock stretch), used to ensure data setup time in master mode: unused in this mode. bit 3-0 sspm<3:0>: synchronous serial port mode select bits 1111 = i 2 c slave mode, 10-bit address with start and stop bit interrupts enabled 1110 = i 2 c slave mode, 7-bit address with start and stop bit interrupts enabled 1011 = i 2 c firmware controlled master mode (slave idle) 1000 = i 2 c master mode, clock = f osc /(4 * (sspadd + 1)) 0111 = i 2 c slave mode, 10-bit address 0110 = i 2 c slave mode, 7-bit address bit combinations not specifically listed here are either reserved or implemented in spi mode only. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 139 pic18(l)f1xk22 register 14-5: sspcon2: ms sp control register (i 2 c mode) r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 gcen ackstat ackdt (2) acken (1) rcen (1) pen (1) rsen (1) sen (1) bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 gcen: general call enable bit (slave mode only) 1 = generate interrupt when a general call address 0x00 or 00h is received in the sspsr 0 = general call address disabled bit 6 ackstat: acknowledge status bit (master transmit mode only) 1 = acknowledge was not received from slave 0 = acknowledge was received from slave bit 5 ackdt: acknowledge data bit (master receive mode only) (2) 1 = not acknowledge 0 = acknowledge bit 4 acken: acknowledge sequence enable bit (master receive mode only) (1) 1 = initiate acknowledge sequence on sda and scl pins and transmit ackdt data bit. automatically cleared by hardware. 0 = acknowledge sequence idle bit 3 rcen: receive enable bit (master mode only) (1) 1 = enables receive mode for i 2 c 0 = receive idle bit 2 pen: stop condition enable bit (master mode only) (1) 1 = initiate stop condition on sda and scl pins. automatically cleared by hardware. 0 = stop condition idle bit 1 rsen: repeated start condition enable bit (master mode only) (1) 1 = initiate repeated start condition on sda and scl pins. automatically cleared by hardware. 0 = repeated start condition idle bit 0 sen: start condition enable/stretch enable bit (1) in master mode: 1 = initiate start condition on sda and scl pins. automatically cleared by hardware. 0 = start condition idle in slave mode: 1 = clock stretching is enabled for both slave transmit and slave receive (stretch enabled) 0 = clock stretching is disabled note 1: for bits acken, rcen, pen, rsen, sen: if the i 2 c module is not in the idle mode, these bits may not be set (no spooling) and the sspbuf may not be written (or writes to the sspbuf are disabled). 2: value that will be transmitted when the user initiates an acknowledge sequence at the end of a receive. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 140 ? 2009-2016 microchip technology inc. 14.3.2 operation the mssp module functions are enabled by setting sspen bit of the sspcon1 register. the sspcon1 register allows control of the i 2 c operation. four mode selection bits of the sspcon1 register allow one of the following i 2 c modes to be selected: i 2 c master mode, clock = (f osc /(4*(sspadd + 1)) i 2 c slave mode (7-bit address) i 2 c slave mode (10-bit address) i 2 c slave mode (7-bit address) with start and stop bit interrupts enabled i 2 c slave mode (10-bit address) with start and stop bit interrupts enabled i 2 c firmware controlled master mode, slave is idle selection of any i 2 c mode with the sspen bit set, forces the scl and sda pins to be open-drain, provided these pins are programmed to inputs by setting the appropriate tris bits 14.3.3 slave mode in slave mode, the scl and sda pins must be configured as inputs. the mssp module will override the input state with the output data when required (slave-transmitter). the i 2 c slave mode hardware will always generate an interrupt on an address match. through the mode select bits, the user can also choose to interrupt on start and stop bits when an address is matched, or the data transfer after an address match is received, the hardware automatically will generate the acknowledge (ack ) pulse and load the sspbuf register with the received value currently in the sspsr register. any combination of the following conditions will cause the mssp module not to give this ack pulse: the buffer full bit, bf bit of the sspstat register, is set before the transfer is received. the overflow bit, sspov bit of the sspcon1 register, is set before the transfer is received. in this case, the sspsr register value is not loaded into the sspbuf, but bit sspif of the pir1 register is set. the bf bit is cleared by reading the sspbuf register, while bit sspov is cleared through software. the scl clock input must have a minimum high and low for proper operation. the high and low times of the i 2 c specification, as well as the requirement of the mssp module, are shown in section 26.0 ?electrical specifications? . 14.3.3.1 addressing once the mssp module has been enabled, it waits for a start condition to occur. following the start condition, the eight bits are shifted into the sspsr register. all incoming bits are sampled with the rising edge of the clock (scl) line. the value of register sspsr<7:1> is compared to the value of the sspadd register. the address is compared on the falling edge of the eighth clock (scl) pulse. if the addresses match and the bf and sspov bits are clear, the following events occur: 1. the sspsr register value is loaded into the sspbuf register. 2. the buffer full bit, bf, is set. 3. an ack pulse is generated. 4. mssp interrupt flag bit, sspif of the pir1 register, is set (interrupt is generated, if enabled) on the falling edge of the ninth scl pulse. in 10-bit address mode, two address bytes need to be received by the slave. the five most significant bits (msbs) of the first address byte specify if this is a 10-bit address. bit r/w of the sspstat register must specify a write so the slave device will receive the second address byte. for a 10-bit address, the first byte would equal 11110 a9 a8 0 , where a9 and a8 are the two msbs of the address. the sequence of events for 10-bit address is as follows, with steps 7 through 9 for the slave-transmitter: 1. receive first (high) byte of address (bits sspif, bf and ua of the sspstat register are set). 2. read the sspbuf register (clears bit bf) and clear flag bit, sspif. 3. update the sspadd register with second (low) byte of address (clears bit ua and releases the scl line). 4. receive second (low) byte of address (bits sspif, bf and ua are set). if the address matches then the scl is held until the next step. otherwise the scl line is not held. 5. read the sspbuf register (clears bit bf) and clear flag bit, sspif. 6. update the sspadd register with the first (high) byte of address. (this will clear bit ua and release a held scl line.) 7. receive repeated start condition. 8. receive first (high) byte of address with r/w bit set (bits sspif, bf, r/w are set). 9. read the sspbuf register (clears bit bf) and clear flag bit, sspif. 10. load sspbuf with byte the slave is to transmit, sets the bf bit. 11. set the ckp bit to release scl. note: to ensure proper operation of the module, pull-up resistors must be provided externally to the scl and sda pins. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 141 pic18(l)f1xk22 14.3.3.2 reception when the r/w bit of the address byte is clear and an address match occurs, the r/w bit of the sspstat register is cleared. the received address is loaded into the sspbuf register and the sda line is held low (ack ). when the address byte overflow condition exists, then the no acknowledge (ack ) pulse is given. an overflow condition is defined as either bit bf bit of the sspstat register is set, or bit sspov bit of the sspcon1 register is set. an mssp interrupt is generated for each data transfer byte. flag bit, sspif of the pir1 register, must be cleared by software. when the sen bit of the sspcon2 register is set, scl will be held low (clock stretch) following each data transfer. the clock must be released by setting the ckp bit of the sspcon1 register. see section 14.3.4 ?clock stretching? for more detail. 14.3.3.3 transmission when the r/w bit of the incoming address byte is set and an address match occurs, the r/w bit of the sspstat register is set. the received address is loaded into the sspbuf register. the ack pulse will be sent on the ninth bit and pin sck/scl is held low regardless of sen (see section 14.3.4 ?clock stretching? for more detail). by stretching the clock, the master will be unable to assert another clock pulse until the slave is done preparing the transmit data. the transmit data must be loaded into the sspbuf register which also loads the sspsr register. then pin sck/scl should be released by setting the ckp bit of the sspcon1 register. the eight data bits are shifted out on the falling edge of the scl input. this ensures that the sda signal is valid during the scl high time ( figure 14-9 ). the ack pulse from the master-receiver is latched on the rising edge of the ninth scl input pulse. if the sda line is high (not ack ), then the data transfer is complete. in this case, when the ack is latched by the slave, the slave logic is reset (resets sspstat register) and the slave monitors for another occurrence of the start bit. if the sda line was low (ack ), the next transmit data must be loaded into the sspbuf register. again, pin sck/scl must be released by setting bit ckp. an mssp interrupt is generated for each data transfer byte. the sspif bit must be cleared by software and the sspstat register is used to determine the status of the byte. the sspif bit is set on the falling edge of the ninth clock pulse. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 142 ? 2009-2016 microchip technology inc. figure 14-8: i 2 c slave mode timing with sen = 0 (reception, 7-bit address) sda scl sspif bf (sspstat<0>) sspov (sspcon1<6>) s 12345678912345678912345 789 p a7 a6 a5 a4 a3 a2 a1 d7 d6 d5 d4 d3 d2 d1 d0 d7 d6 d5 d4 d3 d1 d0 ack receiving data ack receiving data r/w = 0 ack receiving address cleared by software sspbuf is read bus master terminates transfer sspov is set because sspbuf is still full. ack is not sent. d2 6 (pir1<3>) ckp (ckp does not reset to 0 when sen = 0 ) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 143 pic18(l)f1xk22 figure 14-9: i 2 c slave mode timing (transmission, 7-bit address) sdascl sspif (pir1<3>) bf (sspstat<0>) a6 a5 a4 a3 a2 a1 d6 d5 d4 d3 d2 d1 d0 1 2 3 4 5 6 7 8 2 3 4 5 6 7 8 9 sspbuf is written by software cleared by software from sspif isr data in sampled s ack transmitting data r/w = 0 ack receiving address a7 d7 9 1 d6 d5 d4 d3 d2 d1 d0 2 3 4 5 6 7 8 9 sspbuf is written by software cleared by software from sspif isr transmitting data d7 1 ckp p ack ckp is set by software ckp is set by software scl held low while cpu responds to sspif sspbuf is read by software bus master terminates software downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 144 ? 2009-2016 microchip technology inc. figure 14-10: i 2 c slave mode timing with sen = 0 (reception, 10-bit address) sdascl sspif bf (sspstat<0>) s 123456789 123456789 12345 789 p 11110 a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 d7 d6 d5 d4 d3 d1 d0 receive data byte ack r/w = 0 ack receive first byte of address cleared by software d2 6 (pir1<3>) cleared by software receive second byte of address cleared by hardware when sspadd is updated with low byte of address ua (sspstat<1>) clock is held low until update of sspadd has taken place ua is set indicating that the sspadd needs to be updated ua is set indicating that sspadd needs to be updated cleared by hardware when sspadd is updated with high byte of address sspbuf is written with contents of sspsr dummy read of sspbuf to clear bf flag ack ckp 12345 789 d7 d6 d5 d4 d3 d1 d0 receive data byte bus master terminates transfer d2 6 ack cleared by software cleared by software sspov (sspcon1<6>) sspov is set because sspbuf is still full. ack is not sent. (ckp does not reset to 0 when sen = 0 ) clock is held low until update of sspadd has taken place downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 145 pic18(l)f1xk22 figure 14-11: i 2 c slave mode timing (transmission, 10-bit address) sdascl sspif bf s 1 234 56 7 89 1 2345 67 89 1 2345 7 89 p 11110 a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 11110 a8 r/w =1 ack ack r/w = 0 ack receive first byte of address cleared in software bus master sends stop condition a9 6 receive second byte of address cleared by hardware when sspadd is updated with low byte of address. ua clock is held low until update of sspadd has taken place ua is set indicating that the sspadd needs to be updated ua is set indicating that sspadd needs to be updated cleared by hardware when sspadd is updated with high byte of address. sspbuf is written with contents of sspsr dummy read of sspbuf to clear bf flag receive first byte of address 12345 789 d7 d6 d5 d4 d3 d1 ack d2 6 transmitting data byte d0 dummy read of sspbuf to clear bf flag sr cleared in software write of sspbuf cleared in software completion of clears bf flag ckp ckp is set in software, initiates transmission ckp is automatically cleared in hardware holding scl low clock is held low until update of sspadd has taken place data transmission clock is held low until ckp is set to 1 bus master sends restarts condition dummy read of sspbuf to clear bf flag downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 146 ? 2009-2016 microchip technology inc. 14.3.3.4 ssp mask register an ssp mask (sspmsk) register is available in i 2 c slave mode as a mask for the value held in the sspsr register during an address comparison operation. a zero ( 0 ) bit in the sspmsk register has the effect of making the corresponding bit in the sspsr register a dont care. this register is reset to all 1 s upon any reset condition and, therefore, has no effect on standard ssp operation until written with a mask value. this register must be initiated prior to setting sspm<3:0> bits to select the i 2 c slave mode (7-bit or 10-bit address). the ssp mask register is active during: 7-bit address mode: address compare of a<7:1>. 10-bit address mode: address compare of a<7:0> only. the ssp mask has no effect during the reception of the first (high) byte of the address. register 14-6: sspmsk: ssp mask register r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 msk7 msk6 msk5 msk4 msk3 msk2 msk1 msk0 (1) bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-1 msk<7:1>: mask bits 1 = the received address bit n is compared to sspadd to detect i 2 c address match 0 = the received address bit n is not used to detect i 2 c address match bit 0 msk<0>: mask bit for i 2 c slave mode, 10-bit address (1) i 2 c slave mode, 10-bit address (sspm<3:0> = 0111 ): 1 = the received address bit 0 is compared to sspadd<0> to detect i 2 c address match 0 = the received address bit 0 is not used to detect i 2 c address match note 1: the msk0 bit is used only in 10-bit slave mode. in all other modes, this bit has no effect. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 147 pic18(l)f1xk22 register 14-7: sspadd: mssp address and baud rate register (i 2 c mode) r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 add7 add6 add5 add4 add3 add2 add1 add0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown master mode : bit 7-0 add<7:0>: baud rate clock divider bits scl pin clock period = ((add<7:0> + 1) *4)/f osc 10-bit slave mode ? m ost si gnificant ad dress by te : bit 7-3 not used: unused for most significant address byte. bit state of this register is a dont care. bit pattern sent by master is fixed by i 2 c specification and must be equal to 11110 . however, those bits are compared by hardware and are not affected by the value in this register. bit 2-1 add<9:8>: two most significant bits of 10-bit address bit 0 not used: unused in this mode. bit state is a dont care. 10-bit slave mode ? l east si gnificant ad dress by te : bit 7-0 add<7:0>: eight least significant bits of 10-bit address 7-bit slave mode : bit 7-1 add<6:0>: 7-bit address bit 0 not used: unused in this mode. bit state is a dont care. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 148 ? 2009-2016 microchip technology inc. 14.3.4 clock stretching both 7-bit and 10-bit slave modes implement automatic clock stretching during a transmit sequence. the sen bit of the sspcon2 register allows clock stretching to be enabled during receives. setting sen will cause the scl pin to be held low at the end of each data receive sequence. 14.3.4.1 clock stretching for 7-bit slave receive mode (sen = 1 ) in 7-bit slave receive mode, on the falling edge of the ninth clock at the end of the ack sequence if the bf bit is set, the ckp bit of the sspcon1 register is automatically cleared, forcing the scl output to be held low. the ckp being cleared to 0 will assert the scl line low. the ckp bit must be set in the users isr before reception is allowed to continue. by holding the scl line low, the user has time to service the isr and read the contents of the sspbuf before the master device can initiate another data transfer sequence. this will prevent buffer overruns from occurring (see figure 14-13 ). 14.3.4.2 clock stretching for 10-bit slave receive mode (sen = 1 ) in 10-bit slave receive mode during the address sequence, clock stretching automatically takes place but ckp is not cleared. during this time, if the ua bit is set after the ninth clock, clock stretching is initiated. the ua bit is set after receiving the upper byte of the 10-bit address and following the receive of the second byte of the 10-bit address with the r/w bit cleared to 0 . the release of the clock line occurs upon updating sspadd. clock stretching will occur on each data receive sequence as described in 7-bit mode. 14.3.4.3 clock stretching for 7-bit slave transmit mode 7-bit slave transmit mode implements clock stretching by clearing the ckp bit after the falling edge of the ninth clock. this occurs regardless of the state of the sen bit. the users isr must set the ckp bit before transmission is allowed to continue. by holding the scl line low, the user has time to service the isr and load the contents of the sspbuf before the master device can initiate another data transfer sequence (see figure 14-9 ). 14.3.4.4 clock stretching for 10-bit slave transmit mode in 10-bit slave transmit mode, clock stretching is controlled during the first two address sequences by the state of the ua bit, just as it is in 10-bit slave receive mode. the first two addresses are followed by a third address sequence which contains the high-order bits of the 10-bit address and the r/w bit set to 1 . after the third address sequence is performed, the ua bit is not set, the module is now configured in transmit mode and clock stretching is automatic with the hardware clearing ckp, as in 7-bit slave transmit mode (see figure 14-11 ). note 1: if the user reads the contents of the sspbuf before the falling edge of the ninth clock, thus clearing the bf bit, the ckp bit will not be cleared and clock stretching will not occur. 2: the ckp bit can be set by software regardless of the state of the bf bit. the user should be careful to clear the bf bit in the isr before the next receive sequence in order to prevent an overflow condition. note 1: if the user loads the contents of sspbuf, setting the bf bit before the falling edge of the ninth clock, the ckp bit will not be cleared and clock stretching will not occur. 2: the ckp bit can be set by software regardless of the state of the bf bit. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 149 pic18(l)f1xk22 14.3.4.5 clock synchronization and the ckp bit when the ckp bit is cleared, the scl output is forced to 0 . however, clearing the ckp bit will not assert the scl output low until the scl output is already sampled low. therefore, the ckp bit will not assert the scl line until an external i 2 c master device has already asserted the scl line. the scl output will remain low until the ckp bit is set and all other devices on the i 2 c bus have deasserted scl. this ensures that a write to the ckp bit will not violate the minimum high time requirement for scl (see figure 14-12 ). figure 14-12: clock synchronization timing sda scl dx C 1 dx wr q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 sspcon1 ckp master device deasserts clock master device asserts clock downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 150 ? 2009-2016 microchip technology inc. figure 14-13: i 2 c slave mode timing with sen = 1 (reception, 7-bit address) sdascl sspif bf (sspstat<0>) sspov (sspcon1<6>) s 1 234 56 7 8 9 1 2345 67 89 1 23 45 7 89 p a7 a6 a5 a4 a3 a2 a1 d7 d6 d5 d4 d3 d2 d1 d0 d7 d6 d5 d4 d3 d1 d0 ack receiving data ack receiving data r/w = 0 ack receiving address cleared by software sspbuf is read bus master terminates transfer sspov is set because sspbuf is still full. ack is not sent. d2 6 (pir1<3>) ckp ckp written to 1 in if bf is cleared prior to the falling edge of the 9th clock, ckp will not be reset to 0 and no clock stretching will occur software clock is held low until ckp is set to 1 clock is not held low because buffer full bit is clear prior to falling edge of 9th clock clock is not held low because ack = 1 bf is set after falling edge of the 9th clock, ckp is reset to 0 and clock stretching occurs downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 151 pic18(l)f1xk22 figure 14-14: i 2 c slave mode timing with sen = 1 (reception, 10-bit address) sda scl sspif bf (sspstat<0>) s 123456789 123456789 12345 789 p 11110 a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 d7 d6 d5 d4 d3 d1 d0 receive data byte ack r/w = 0 ack receive first byte of address cleared by software d2 6 (pir1<3>) cleared by software receive second byte of address cleared by hardware when sspadd is updated with low byte of address after falling edge ua (sspstat<1>) clock is held low until update of sspadd has taken place ua is set indicating that the sspadd needs to be updated ua is set indicating that sspadd needs to be updated cleared by hardware when sspadd is updated with high byte of address after falling edge sspbuf is written with contents of sspsr dummy read of sspbuf to clear bf flag ack ckp 12345 789 d7 d6 d5 d4 d3 d1 d0 receive data byte bus master terminates transfer d2 6 ack cleared by software cleared by software sspov (sspcon1<6>) ckp written to 1 note: an update of the sspadd register before the falling edge of the ninth clock will have no effect on ua and ua will remain set. note: an update of the sspadd register before the falling edge of the ninth clock will have no effect on ua and ua will remain set. by software clock is held low until update of sspadd has taken place of ninth clock of ninth clock sspov is set because sspbuf is still full. ack is not sent. dummy read of sspbuf to clear bf flag clock is held low until ckp is set to 1 clock is not held low because ack = 1 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 152 ? 2009-2016 microchip technology inc. 14.3.5 general call address support the addressing procedure for the i 2 c bus is such that the first byte after the start condition usually determines which device will be the slave addressed by the master. the exception is the general call address which can address all devices. when this address is used, all devices should, in theory, respond with an acknowledge. the general call address is one of eight addresses reserved for specific purposes by the i 2 c protocol. it consists of all 0 s with r/w = 0 . the general call address is recognized when the gcen bit of the sspcon2 is set. following a start bit detect, eight bits are shifted into the sspsr and the address is compared against the sspadd. it is also compared to the general call address and fixed in hardware. if the general call address matches, the sspsr is transferred to the sspbuf, the bf flag bit is set (eighth bit) and on the falling edge of the ninth bit (ack bit), the sspif interrupt flag bit is set. when the interrupt is serviced, the source for the interrupt can be checked by reading the contents of the sspbuf. the value can be used to determine if the address was device specific or a general call address. in 10-bit mode, the sspadd is required to be updated for the second half of the address to match and the ua bit of the sspstat register is set. if the general call address is sampled when the gcen bit is set, while the slave is configured in 10-bit address mode, then the second half of the address is not necessary, the ua bit will not be set and the slave will begin receiving data after the acknowledge ( figure 14-15 ). figure 14-15: slave mode general call address sequence (7 or 10-bit address mode) sda scl s sspif bf (sspstat<0>) sspov (sspcon1<6>) cleared by software sspbuf is read r/w = 0 ack general call address address is compared to general call address gcen (sspcon2<7>) receiving data ack 123456789123456789 d7 d6 d5 d4 d3 d2 d1 d0 after ack , set interrupt 0 1 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 153 pic18(l)f1xk22 14.3.6 master mode master mode is enabled by setting and clearing the appropriate sspm bits in sspcon1 and by setting the sspen bit. in master mode, the scl and sda lines are manipulated by the mssp hardware. master mode of operation is supported by interrupt generation on the detection of the start and stop conditions. the stop (p) and start (s) bits are cleared from a reset or when the mssp module is disabled. control of the i 2 c bus may be taken when the p bit is set, or the bus is idle, with both the s and p bits clear. in firmware controlled master mode, user code conducts all i 2 c bus operations based on start and stop bit conditions. once master mode is enabled, the user has six options. 1. assert a start condition on sda and scl. 2. assert a repeated start condition on sda and scl. 3. write to the sspbuf register initiating transmission of data/address. 4. configure the i 2 c port to receive data. 5. generate an acknowledge condition at the end of a received byte of data. 6. generate a stop condition on sda and scl. the following events will cause the ssp interrupt flag bit, sspif, to be set (ssp interrupt, if enabled): start condition stop condition data transfer byte transmitted/received acknowledge transmit repeated start figure 14-16: mssp block diagram (i 2 c master mode) note: the mssp module, when configured in i 2 c master mode, does not allow queueing of events. for instance, the user is not allowed to initiate a start condition and immediately write the sspbuf register to initiate transmission before the start condition is complete. in this case, the sspbuf will not be written to and the wcol bit will be set, indicating that a write to the sspbuf did not occur. read write sspsr start bit, stop bit, sspbuf internal data bus set/reset, s, p, wcol shift clock msb lsb sda acknowledge generate stop bit detect write collision detect clock arbitration state counter for end of xmit/rcv scl scl in bus collision sda in receive enable clock cntl clock arbitrate/wcol detect (hold off clock source) sspadd<6:0> baud set sspif, bclif reset ackstat, pen rate generator sspm<3:0> start bit detect downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 154 ? 2009-2016 microchip technology inc. 14.3.6.1 i 2 c master mode operation the master device generates all of the serial clock pulses and the start and stop conditions. a transfer is ended with a stop condition or with a repeated start condition. since the repeated start condition is also the beginning of the next serial transfer, the i 2 c bus will not be released. in master transmitter mode, serial data is output through sda, while scl outputs the serial clock. the first byte transmitted contains the slave address of the receiving device (seven bits) and the read/write (r/w ) bit. in this case, the r/w bit will be logic 0 . serial data is transmitted eight bits at a time. after each byte is transmitted, an acknowledge bit is received. start and stop conditions are output to indicate the beginning and the end of a serial transfer. in master receive mode, the first byte transmitted contains the slave address of the transmitting device (7 bits) and the r/w bit. in this case, the r/w bit will be logic 1 . thus, the first byte transmitted is a 7-bit slave address followed by a 1 to indicate the receive bit. serial data is received via sda, while scl outputs the serial clock. serial data is received 8 bits at a time. after each byte is received, an acknowledge bit is transmitted. start and stop conditions indicate the beginning and end of transmission. a baud rate generator is used to set the clock frequency output on scl. see section 14.3.7 ?baud rate? for more detail. a typical transmit sequence would go as follows: 1. the user generates a start condition by setting the sen bit of the sspcon2 register. 2. sspif is set. the mssp module will wait the required start time before any other operation takes place. 3. the user loads the sspbuf with the slave address to transmit. 4. address is shifted out the sda pin until all 8 bits are transmitted. 5. the mssp module shifts in the ack bit from the slave device and writes its value into the ackstat bit of the sspcon2 register. 6. the mssp module generates an interrupt at the end of the ninth clock cycle by setting the sspif bit. 7. the user loads the sspbuf with eight bits of data. 8. data is shifted out the sda pin until all 8 bits are transmitted. 9. the mssp module shifts in the ack bit from the slave device and writes its value into the ackstat bit of the sspcon2 register. 10. the mssp module generates an interrupt at the end of the ninth clock cycle by setting the sspif bit. 11. the user generates a stop condition by setting the pen bit of the sspcon2 register. 12. interrupt is generated once the stop condition is complete. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 155 pic18(l)f1xk22 14.3.7 baud rate in i 2 c master mode, the baud rate generator (brg) reload value is placed in the sspadd register ( figure 14-17 ). when a write occurs to sspbuf, the baud rate generator will automatically begin counting. once the given operation is complete (i.e., transmission of the last data bit is followed by ack ), the internal clock will automatically stop counting and the scl pin will remain in its last state. table 14-3 demonstrates clock rates based on instruction cycles and the brg value loaded into sspadd. equation 14-1: clock rates figure 14-17: baud rate generato r block diagram table 14-3: i 2 c clock rate w/brg f scl f osc sspadd 1 + ?? 4 ?? ---------------------------------------------- = sspm<3:0> brg down counter clkout f osc /2 sspadd<7:0> sspm<3:0> scl reload control reload f osc f cy brg value f scl (2 rollovers of brg) 48 mhz 12 mhz 0bh 1 mhz (1) 48 mhz 12 mhz 1dh 400 khz 48 mhz 12 mhz 77h 100 khz 40 mhz 10 mhz 18h 400 khz (1) 40 mhz 10 mhz 1fh 312.5 khz 40 mhz 10 mhz 63h 100 khz 16 mhz 4 mhz 09h 400 khz (1) 16 mhz 4 mhz 0ch 308 khz 16 mhz 4 mhz 27h 100 khz 4 mhz 1 mhz 02h 333 khz (1) 4 mhz 1 mhz 09h 100 khz 4 mhz 1 mhz 00h 1 mhz (1) note 1: the i 2 c interface does not conform to the 400 khz i 2 c specification (which applies to rates greater than 100 khz) in all details, but may be used with care where higher rates are required by the application. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 156 ? 2009-2016 microchip technology inc. 14.3.7.1 clock arbitration clock arbitration occurs when the master, during any receive, transmit or repeated start/stop condition, deasserts the scl pin (scl allowed to float high). when the scl pin is allowed to float high, the baud rate generator (brg) is suspended from counting until the scl pin is actually sampled high. when the scl pin is sampled high, the baud rate generator is reloaded with the contents of sspadd<6:0> and begins counting. this ensures that the scl high time will always be at least one brg rollover count in the event that the clock is held low by an external device ( figure 14-18 ). figure 14-18: baud rate generator timing with clock arbitration sda scl scl deasserted but slave holds dx C 1 dx brg scl is sampled high, reload takes place and brg starts its count 03h 02h 01h 00h (hold off) 03h 02h reload brg value scl low (clock arbitration) scl allowed to transition high brg decrements on q2 and q4 cycles downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 157 pic18(l)f1xk22 14.3.8 i 2 c master mode start condition timing to initiate a start condition, the user sets the start enable bit, sen bit of the sspcon2 register. if the sda and scl pins are sampled high, the baud rate generator is reloaded with the contents of sspadd<6:0> and starts its count. if scl and sda are both sampled high when the baud rate generator times out (t brg ), the sda pin is driven low. the action of the sda being driven low while scl is high is the start condition and causes the s bit of the sspstat1 register to be set. following this, the baud rate generator is reloaded with the contents of sspadd<7:0> and resumes its count. when the baud rate generator times out (t brg ), the sen bit of the sspcon2 register will be automatically cleared by hardware; the baud rate generator is suspended, leaving the sda line held low and the start condition is complete. 14.3.8.1 wcol status flag if the user writes the sspbuf when a start sequence is in progress, the wcol is set and the contents of the buffer are unchanged (the write doesnt occur). figure 14-19: first start bit timing note: if at the beginning of the start condition, the sda and scl pins are already sampled low, or if during the start condition, the scl line is sampled low before the sda line is driven low, a bus collision occurs, the bus collision interrupt flag, bclif, is set, the start condition is aborted and the i 2 c module is reset into its idle state. note: because queuing of events is not allowed, writing to the lower five bits of sspcon2 is disabled until the start condition is complete. sda scl s t brg 1st bit 2nd bit t brg sda = 1 , at completion of start bit, scl = 1 write to sspbuf occurs here t brg hardware clears sen bit t brg write to sen bit occurs here set s bit (sspstat<3>) and sets sspif bit downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 158 ? 2009-2016 microchip technology inc. 14.3.9 i 2 c master mode repeated start condition timing a repeated start condition occurs when the rsen bit of the sspcon2 register is programmed high and the i 2 c logic module is in the idle state. when the rsen bit is set, the scl pin is asserted low. when the scl pin is sampled low, the baud rate generator is loaded and begins counting. the sda pin is released (brought high) for one baud rate generator count (t brg ). when the baud rate generator times out, if sda is sampled high, the scl pin will be deasserted (brought high). when scl is sampled high, the baud rate generator is reloaded and begins counting. sda and scl must be sampled high for one t brg . this action is then followed by assertion of the sda pin (sda = 0 ) for one t brg while scl is high. following this, the rsen bit of the sspcon2 register will be automatically cleared and the baud rate generator will not be reloaded, leaving the sda pin held low. as soon as a start condition is detected on the sda and scl pins, the s bit of the sspstat register will be set. the sspif bit will not be set until the baud rate generator has timed out. immediately following the sspif bit getting set, the user may write the sspbuf with the 7-bit address in 7-bit mode or the default first address in 10-bit mode. after the first eight bits are transmitted and an ack is received, the user may then transmit an additional eight bits of address (10-bit mode) or eight bits of data (7-bit mode). 14.3.9.1 wcol status flag if the user writes the sspbuf when a repeated start sequence is in progress, the wcol is set and the contents of the buffer are unchanged (the write doesnt occur). figure 14-20: repeat start condition waveform note 1: if rsen is programmed while any other event is in progress, it will not take effect. 2: a bus collision during the repeated start condition occurs if: sda is sampled low when scl goes from low-to-high. scl goes low before sda is asserted low. this may indicate that another master is attempting to transmit a data 1 . note: because queueing of events is not allowed, writing of the lower 5 bits of ssp- con2 is disabled until the repeated start condition is complete. sdascl sr = repeated start write to sspcon2 write to sspbuf occurs here on falling edge of ninth clock, end of xmit at completion of start bit, hardware clears rsen bit 1st bit s bit set by hardware t brg t brg sda = 1 , sda = 1 , scl (no change). scl = 1 occurs here. t brg t brg t brg and sets sspif rsen bit set by hardware downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 159 pic18(l)f1xk22 14.3.10 i 2 c master mode transmission transmission of a data byte, a 7-bit address or the other half of a 10-bit address is accomplished by simply writing a value to the sspbuf register. this action will set the buffer full flag bit, bf, and allow the baud rate generator to begin counting and start the next transmission. each bit of address/data will be shifted out onto the sda pin after the falling edge of scl is asserted (see data hold time specification parameter sp106). scl is held low for one baud rate generator rollover count (t brg ). data should be valid before scl is released high (see data setup time specification parameter sp107). when the scl pin is released high, it is held that way for t brg . the data on the sda pin must remain stable for that duration and some hold time after the next falling edge of scl. after the eighth bit is shifted out (the falling edge of the eighth clock), the bf flag is cleared and the master releases sda. this allows the slave device being addressed to respond with an ack bit during the ninth bit time if an address match occurred, or if data was received properly. the status of ack is written into the ackdt bit on the falling edge of the ninth clock. if the master receives an acknowledge, the acknowledge status bit, ackstat, is cleared. if not, the bit is set. after the ninth clock, the sspif bit is set and the master clock (baud rate generator) is suspended until the next data byte is loaded into the sspbuf, leaving scl low and sda unchanged ( figure 14-21 ). after the write to the sspbuf, each bit of the address will be shifted out on the falling edge of scl until all seven address bits and the r/w bit are completed. on the falling edge of the eighth clock, the master will deassert the sda pin, allowing the slave to respond with an acknowledge. on the falling edge of the ninth clock, the master will sample the sda pin to see if the address was recognized by a slave. the status of the ack bit is loaded into the ackstat status bit of the sspcon2 register. following the falling edge of the ninth clock transmission of the address, the sspif is set, the bf flag is cleared and the baud rate generator is turned off until another write to the sspbuf takes place, holding scl low and allowing sda to float. 14.3.10.1 bf status flag in transmit mode, the bf bit of the sspstat register is set when the cpu writes to sspbuf and is cleared when all 8 bits are shifted out. 14.3.10.2 wcol status flag if the user writes the sspbuf when a transmit is already in progress (i.e., sspsr is still shifting out a data byte), the wcol is set and the contents of the buffer are unchanged (the write doesnt occur). wcol must be cleared by software before the next transmission. 14.3.10.3 ackstat status flag in transmit mode, the ackstat bit of the sspcon2 register is cleared when the slave has sent an acknowledge (ack = 0 ) and is set when the slave does not acknowledge (ack = 1 ). a slave sends an acknowledge when it has recognized its address (including a general call), or when the slave has properly received its data. 14.3.11 i 2 c master mode reception master mode reception is enabled by programming the receive enable bit, rcen bit of the sspcon2 register. the baud rate generator begins counting and on each rollover, the state of the scl pin changes (high-to-low/low-to-high) and data is shifted into the sspsr. after the falling edge of the eighth clock, the receive enable flag is automatically cleared, the contents of the sspsr are loaded into the sspbuf, the bf flag bit is set, the sspif flag bit is set and the baud rate generator is suspended from counting, holding scl low. the mssp is now in idle state awaiting the next command. when the buffer is read by the cpu, the bf flag bit is automatically cleared. the user can then send an acknowledge bit at the end of reception by setting the acknowledge sequence enable, acken bit of the sspcon2 register. 14.3.11.1 bf status flag in receive operation, the bf bit is set when an address or data byte is loaded into sspbuf from sspsr. it is cleared when the sspbuf register is read. 14.3.11.2 sspov status flag in receive operation, the sspov bit is set when 8 bits are received into the sspsr and the bf flag bit is already set from a previous reception. 14.3.11.3 wcol status flag if the user writes the sspbuf when a receive is already in progress (i.e., sspsr is still shifting in a data byte), the wcol bit is set and the contents of the buffer are unchanged (the write doesnt occur). note: the mssp module must be in an idle state before the rcen bit is set or the rcen bit will be disregarded. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 160 ? 2009-2016 microchip technology inc. figure 14-21: i 2 c master mode waveform (transmission, 7 or 10-bit address) sdascl sspif bf (sspstat<0>) sen a7 a6 a5 a4 a3 a2 a1 ack = 0 d7 d6 d5 d4 d3 d2 d1 d0 ack transmitting data or second half r/w = 0 transmit address to slave 123456789 123456789 p cleared by software service routine sspbuf is written by software from ssp interrupt after start condition, sen cleared by hardware s sspbuf written with 7-bit address and r/w start transmit scl held low while cpu responds to sspif sen = 0 of 10-bit address write sspcon2<0> sen = 1 start condition begins from slave, clear ackstat bit sspcon2<6> ackstat in sspcon2 = 1 cleared by software sspbuf written pen r/w cleared by software downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 161 pic18(l)f1xk22 figure 14-22: i 2 c master mode waveform (reception, 7-bit address) p 9 8 7 6 5 d0 d1 d2 d3 d4 d5 d6 d7 s a7 a6 a5 a4 a3 a2 a1 sdascl 12 3 4 5 6 7 8 9 12 3 4 5 678 9 1234 bus master terminates transfer ack receiving data from slave receiving data from slave d0 d1 d2 d3 d4 d5 d6 d7 ack r/w = 0 transmit address to slave sspif bf ack is not sent write to sspcon2<0> (sen = 1 ), write to sspbuf occurs here, ack from slave master configured as a receiver by programming sspcon2<3> (rcen = 1 ) pen bit = 1 written here data shifted in on falling edge of clk cleared by software start xmit sen = 0 sspov sda = 0 , scl = 1 while cpu (sspstat<0>) ack cleared by software cleared by software set sspif interrupt at end of receive set p bit (sspstat<4>) and sspif cleared in software ack from master set sspif at end set sspif interrupt at end of acknowledge sequence set sspif interrupt at end of acknow- ledge sequence of receive set acken, start acknowledge sequence sspov is set because sspbuf is still full sda = ackdt = 1 rcen cleared automatically rcen = 1 , start next receive write to sspcon2<4> to start acknowledge sequence sda = ackdt (sspcon2<5>) = 0 rcen cleared automatically responds to sspif acken begin start condition cleared by software sda = ackdt = 0 last bit is shifted into sspsr and contents are unloaded into sspbuf rcen master configured as a receiver by programming sspcon2<3> (rcen = 1 ) rcen cleared automatically ack from master sda = ackdt = 0 rcen cleared automatically downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 162 ? 2009-2016 microchip technology inc. 14.3.12 acknowledge sequence timing an acknowledge sequence is enabled by setting the acknowledge sequence enable bit, acken bit of the sspcon2 register. when this bit is set, the scl pin is pulled low and the contents of the acknowledge data bit are presented on the sda pin. if the user wishes to generate an acknowledge, then the ackdt bit should be cleared. if not, the user should set the ackdt bit before starting an acknowledge sequence. the baud rate generator then counts for one rollover period (t brg ) and the scl pin is deasserted (pulled high). when the scl pin is sampled high (clock arbitration), the baud rate generator counts for t brg . the scl pin is then pulled low. following this, the acken bit is automatically cleared, the baud rate generator is turned off and the mssp module then goes into idle mode ( figure 14-23 ). 14.3.12.1 wcol status flag if the user writes the sspbuf when an acknowledge sequence is in progress, then wcol is set and the contents of the buffer are unchanged (the write doesnt occur). 14.3.13 stop condition timing a stop bit is asserted on the sda pin at the end of a receive/transmit by setting the stop sequence enable bit, pen bit of the sspcon2 register. at the end of a receive/transmit, the scl line is held low after the falling edge of the ninth clock. when the pen bit is set, the master will assert the sda line low. when the sda line is sampled low, the baud rate generator is reloaded and counts down to 0 . when the baud rate generator times out, the scl pin will be brought high and one t brg (baud rate generator rollover count) later, the sda pin will be deasserted. when the sda pin is sampled high while scl is high, the p bit of the sspstat register is set. a t brg later, the pen bit is cleared and the sspif bit is set ( figure 14-24 ). 14.3.13.1 wcol status flag if the user writes the sspbuf when a stop sequence is in progress, then the wcol bit is set and the contents of the buffer are unchanged (the write doesnt occur). figure 14-23: acknowledge sequen ce waveform note: t brg = one baud rate generator period. sda scl sspif set at acknowledge sequence starts here, write to sspcon2 acken automatically cleared cleared in t brg t brg the end of receive 8 acken = 1 , ackdt = 0 d0 9 sspif software sspif set at the end of acknowledge sequence cleared in software ack downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 163 pic18(l)f1xk22 figure 14-24: stop cond ition receive or transmit mode 14.3.14 sleep operation while in sleep mode, the i 2 c slave module can receive addresses or data and when an address match or complete byte transfer occurs, wake the processor from sleep (if the mssp interrupt is enabled). 14.3.15 effects of a reset a reset disables the mssp module and terminates the current transfer. 14.3.16 multi-master mode in multi-master mode, the interrupt generation on the detection of the start and stop conditions allows the determination of when the bus is free. the stop (p) and start (s) bits are cleared from a reset or when the mssp module is disabled. control of the i 2 c bus may be taken when the p bit of the sspstat register is set, or the bus is idle, with both the s and p bits clear. when the bus is busy, enabling the ssp interrupt will generate the interrupt when the stop condition occurs. in multi-master operation, the sda line must be monitored for arbitration to see if the signal level is the expected output level. this check is performed by hardware with the result placed in the bclif bit. the states where arbitration can be lost are: address transfer data transfer a start condition a repeated start condition an acknowledge condition 14.3.17 multi -master communication, bus collision and bus arbitration multi-master mode support is achieved by bus arbitration. when the master outputs address/data bits onto the sda pin, arbitration takes place when the master outputs a 1 on sda, by letting sda float high and another master asserts a 0 . when the scl pin floats high, data should be stable. if the expected data on sda is a 1 and the data sampled on the sda pin = 0 , then a bus collision has taken place. the master will set the bus collision interrupt flag, bclif and reset the i 2 c port to its idle state ( figure 14-25 ). if a transmit was in progress when the bus collision occurred, the transmission is halted, the bf flag is cleared, the sda and scl lines are deasserted and the sspbuf can be written to. when the user services the bus collision interrupt service routine and if the i 2 c bus is free, the user can resume communication by asserting a start condition. if a start, repeated start, stop or acknowledge condition was in progress when the bus collision occurred, the condition is aborted, the sda and scl lines are deasserted and the respective control bits in the sspcon2 register are cleared. when the user services the bus collision interrupt service routine and if the i 2 c bus is free, the user can resume communication by asserting a start condition. the master will continue to monitor the sda and scl pins. if a stop condition occurs, the sspif bit will be set. a write to the sspbuf will start the transmission of data at the first data bit, regardless of where the transmitter left off when the bus collision occurred. in multi-master mode, the interrupt generation on the detection of start and stop conditions allows the determination of when the bus is free. control of the i 2 c bus can be taken when the p bit is set in the sspstat register, or the bus is idle and the s and p bits are cleared. sclsda sda asserted low before rising edge of clock write to sspcon2, set pen falling edge of scl = 1 for t brg , followed by sda = 1 for t brg 9th clock scl brought high after t brg note: t brg = one baud rate generator period. t brg t brg after sda sampled high. p bit (sspstat<4>) is set. t brg to setup stop condition ack p t brg pen bit (sspcon2<2>) is cleared by hardware and the sspif bit is set downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 164 ? 2009-2016 microchip technology inc. figure 14-25: bus collision timing for transmit and acknowledge sda scl bclif sda released sda line pulled low by another source sample sda. while scl is high, data doesnt match what is driven bus collision has occurred. set bus collision interrupt (bclif) by the master. by master data changes while scl = 0 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 165 pic18(l)f1xk22 14.3.17.1 bus collision during a start condition during a start condition, a bus collision occurs if: a) sda or scl are sampled low at the beginning of the start condition ( figure 14-26 ). b) scl is sampled low before sda is asserted low ( figure 14-27 ). during a start condition, both the sda and the scl pins are monitored. if the sda pin is already low, or the scl pin is already low, then all of the following occur: the start condition is aborted, the bclif flag is set and the mssp module is reset to its idle state ( figure 14-26 ). the start condition begins with the sda and scl pins deasserted. when the sda pin is sampled high, the baud rate generator is loaded and counts down. if the scl pin is sampled low while sda is high, a bus collision occurs because it is assumed that another master is attempting to drive a data 1 during the start condition. if the sda pin is sampled low during this count, the brg is reset and the sda line is asserted early ( figure 14-28 ). if, however, a 1 is sampled on the sda pin, the sda pin is asserted low at the end of the brg count. the baud rate generator is then reloaded and counts down to 0; if the scl pin is sampled as 0 during this time, a bus collision does not occur. at the end of the brg count, the scl pin is asserted low. figure 14-26: bus collision during start condition (sda only) note: the reason that bus collision is not a factor during a start condition is that no two bus masters can assert a start condition at the exact same time. therefore, one master will always assert sda before the other. this condition does not cause a bus collision because the two masters must be allowed to arbitrate the first address following the start condition. if the address is the same, arbitration must be allowed to continue into the data portion, repeated start or stop conditions. sda scl sen sda sampled low before sda goes low before the sen bit is set. s bit and sspif set because ssp module reset into idle state. sen cleared automatically because of bus collision. s bit and sspif set because set sen, enable start condition if sda = 1 , scl = 1 sda = 0 , scl = 1 . bclif s sspif sda = 0 , scl = 1 . sspif and bclif are cleared by software sspif and bclif are cleared by software set bclif, start condition. set bclif. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 166 ? 2009-2016 microchip technology inc. figure 14-27: bus collision d uring start condition (scl = 0 ) figure 14-28: brg reset due to sda arbitration during start condition sda scl sen bus collision occurs. set bclif. scl = 0 before sda = 0 , set sen, enable start sequence if sda = 1 , scl = 1 t brg t brg sda = 0 , scl = 1 bclif s sspif interrupt cleared by software bus collision occurs. set bclif. scl = 0 before brg time-out, 0 0 0 0 sda scl sen set s less than t brg t brg sda = 0 , scl = 1 bclif s sspif s interrupts cleared by software set sspif sda = 0 , scl = 1 , scl pulled low after brg time-out set sspif 0 sda pulled low by other master. reset brg and assert sda. set sen, enable start sequence if sda = 1 , scl = 1 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 167 pic18(l)f1xk22 14.3.17.2 bus collision during a repeated start condition during a repeated start condition, a bus collision occurs if: a) a low level is sampled on sda when scl goes from low level to high level. b) scl goes low before sda is asserted low, indicating that another master is attempting to transmit a data 1 . when the user deasserts sda and the pin is allowed to float high, the brg is loaded with sspadd and counts down to 0. the scl pin is then deasserted and when sampled high, the sda pin is sampled. if sda is low, a bus collision has occurred (i.e., another master is attempting to transmit a data 0 , figure 14-29 ). if sda is sampled high, the brg is reloaded and begins counting. if sda goes from high-to-low before the brg times out, no bus collision occurs because no two masters can assert sda at exactly the same time. if scl goes from high-to-low before the brg times out and sda has not already been asserted, a bus collision occurs. in this case, another master is attempting to transmit a data 1 during the repeated start condition, see figure 14-30 . if, at the end of the brg time-out, both scl and sda are still high, the sda pin is driven low and the brg is reloaded and begins counting. at the end of the count, regardless of the status of the scl pin, the scl pin is driven low and the repeated start condition is complete. figure 14-29: bus collision during a repeat ed start condition (case 1) figure 14-30: bus collision during repeat ed start condition (case 2) sda scl rsen bclif s sspif sample sda when scl goes high. if sda = 0 , set bclif and release sda and scl. cleared by software 0 0 sda scl bclif rsen s sspif interrupt cleared by software scl goes low before sda, set bclif. release sda and scl. t brg t brg 0 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 168 ? 2009-2016 microchip technology inc. 14.3.17.3 bus collision during a stop condition bus collision occurs during a stop condition if: a) after the sda pin has been deasserted and allowed to float high, sda is sampled low after the brg has timed out. b) after the scl pin is deasserted, scl is sampled low before sda goes high. the stop condition begins with sda asserted low. when sda is sampled low, the scl pin is allowed to float. when the pin is sampled high (clock arbitration), the baud rate generator is loaded with sspadd and counts down to 0. after the brg times out, sda is sampled. if sda is sampled low, a bus collision has occurred. this is due to another master attempting to drive a data 0 ( figure 14-31 ). if the scl pin is sampled low before sda is allowed to float high, a bus collision occurs. this is another case of another master attempting to drive a data 0 ( figure 14-32 ). figure 14-31: bus collision during a stop condition (case 1) figure 14-32: bus collision during a stop condition (case 2) sda scl bclif pen p sspif t brg t brg t brg sda asserted low sda sampled low after t brg , set bclif 0 0 sda scl bclif pen p sspif t brg t brg t brg assert sda scl goes low before sda goes high, set bclif 0 0 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 169 pic18(l)f1xk22 table 14-4: summary of registers associated with i 2 c name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page ipr1 adip rcip txip sspip ccp1ip tmr2ip tmr1ip 248 ipr2 oscfip c1ip c2ip eeip bclip tmr3ip 248 pie1 adie rcie txie sspie ccp1ie tmr2ie tmr1ie 248 pie2 oscfie c1ie c2ie eeie bclie tmr3ie 248 pir1 adif rcif txif sspif ccp1if tmr2if tmr1if 248 pir2 oscfif c1if c2if eeif bclif tmr3if 248 sspadd ssp address register in i 2 c slave mode. ssp baud rate reload register in i 2 c master mode. 246 sspbuf ssp receive buffer/transmit register 246 sspcon1 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 246 sspcon2 gcen ackstat ackdt acken rcen pen rsen sen 246 sspmsk msk7 msk6 msk5 msk4 msk3 msk2 msk1 msk0 248 sspstat smp cke d/a psr / w ua bf 246 trisb trisb7 trisb6 trisb5 trisb4 248 legend: = unimplemented, read as 0. shaded cells are not used by i 2 c. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 170 ? 2009-2016 microchip technology inc. 15.0 enhanced universal synchronous asynchronous receiver transmitter (eusart) the enhanced universal synchronous asynchronous receiver transmitter (eusart) module is a serial i/o communications peripheral. it contains all the clock generators, shift registers and data buffers necessary to perform an input or output serial data transfer independent of device program execution. the eusart, also known as a serial communications interface (sci), can be configured as a full-duplex asynchronous system or half-duplex synchronous system. full-duplex mode is useful for communications with peripheral systems, such as crt terminals and personal computers. half-duplex synchronous mode is intended for communications with peripheral devices, such as a/d or d/a integrated circuits, serial eeproms or other microcontrollers. these devices typically do not have internal clocks for baud rate generation and require the external clock signal provided by a master synchronous device. the eusart module includes the following capabilities: full-duplex asynchronous transmit and receive two-character input buffer one-character output buffer programmable 8-bit or 9-bit character length address detection in 9-bit mode input buffer overrun error detection received character framing error detection half-duplex synchronous master half-duplex synchronous slave programmable clock and data polarity the eusart module implements the following additional features, making it ideally suited for use in local interconnect network (lin) bus systems: automatic detection and calibration of the baud rate wake-up on break reception 13-bit break character transmit block diagrams of the eusart transmitter and receiver are shown in figure 15-1 and figure 15-2 . figure 15-1: eusart transmi t block diagram txif txie interrupt txen tx9d msb lsb data bus txreg register transmit shift register (tsr) (8) 0 tx9 trmt spen tx/ck pin pin buffer and control 8 spbrg spbrgh brg16 f osc n n + 1 multiplier x4 x16 x64 sync 1x00 0 brgh x110 0 brg16 x101 0 baud rate generator downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 171 pic18(l)f1xk22 figure 15-2: eusart receiv e block diagram the operation of the eusart module is controlled through three registers: transmit status and control (txsta) receive status and control (rcsta) baud rate control (baudctl) these registers are detailed in register 15-1 , register 15-2 and register 15-3 , respectively. for all modes of eusart operation, the tris control bits corresponding to the rx/dt and tx/ck pins should be set to 1 . the eusart control will automatically reconfigure the pin from input to output, as needed. rx/dt pin pin buffer and control spen data recovery cren oerr ferr rsr register msb lsb rx9d rcreg register fifo interrupt rcif rcie data bus 8 stop start (8) 7 10 rx9 spbrg spbrgh brg16 rcidl f osc n n + 1 multiplier x4 x16 x64 sync 1x00 0 brgh x110 0 brg16 x101 0 baud rate generator downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 172 ? 2009-2016 microchip technology inc. 15.1 eusart asynchronous mode the eusart transmits and receives data using the standard non-return-to-zero (nrz) format. nrz is implemented with two levels: a v oh mark state which represents a 1 data bit, and a v ol space state which represents a 0 data bit. nrz refers to the fact that consecutively transmitted data bits of the same value stay at the output level of that bit without returning to a neutral level between each bit transmission. an nrz transmission port idles in the mark state. each character transmission consists of one start bit followed by eight or nine data bits and is always terminated by one or more stop bits. the start bit is always a space and the stop bits are always marks. the most common data format is 8 bits. each transmitted bit persists for a period of 1/(baud rate). an on-chip dedicated 8-bit/16-bit baud rate generator is used to derive standard baud rate frequencies from the system oscillator. see table 15-5 for examples of baud rate configurations. the eusart transmits and receives the lsb first. the eusarts transmitter and receiver are functionally independent, but share the same data format and baud rate. parity is not supported by the hardware, but can be implemented in software and stored as the ninth data bit. 15.1.1 eusart asynchronous transmitter the eusart transmitter block diagram is shown in figure 15-1 . the heart of the transmitter is the serial transmit shift register (tsr), which is not directly accessible by software. the tsr obtains its data from the transmit buffer, which is the txreg register. 15.1.1.1 enabling the transmitter the eusart transmitter is enabled for asynchronous operations by configuring the following three control bits: txen = 1 sync = 0 spen = 1 all other eusart control bits are assumed to be in their default state. setting the txen bit of the txsta register enables the transmitter circuitry of the eusart. clearing the sync bit of the txsta register configures the eusart for asynchronous operation. setting the spen bit of the rcsta register enables the eusart and automatically configures the tx/ck i/o pin as an output. if the tx/ck pin is shared with an analog peripheral the analog i/o function must be disabled by clearing the corresponding ansel bit. 15.1.1.2 transmitting data a transmission is initiated by writing a character to the txreg register. if this is the first character, or the previous character has been completely flushed from the tsr, the data in the txreg is immediately transferred to the tsr register. if the tsr still contains all or part of a previous character, the new character data is held in the txreg until the stop bit of the previous character has been transmitted. the pending character in the txreg is then transferred to the tsr in one t cy immediately following the stop bit transmission. the transmission of the start bit, data bits and stop bit sequence commences immediately following the transfer of the data to the tsr from the txreg. 15.1.1.3 transmit data polarity the polarity of the transmit data can be controlled with the cktxp bit of the baudcon register. the default state of this bit is 0 which selects high true transmit idle and data bits. setting the cktxp bit to 1 will invert the transmit data resulting in low true idle and data bits. the cktxp bit controls transmit data polarity only in asynchronous mode. in synchronous mode the cktxp bit has a different function. 15.1.1.4 transmit interrupt flag the txif interrupt flag bit of the pir1 register is set whenever the eusart transmitter is enabled and no character is being held for transmission in the txreg. in other words, the txif bit is only clear when the tsr is busy with a character and a new character has been queued for transmission in the txreg. the txif flag bit is not cleared immediately upon writing txreg. txif becomes valid in the second instruction cycle following the write execution. polling txif immediately following the txreg write will return invalid results. the txif bit is read-only, it cannot be set or cleared by software. the txif interrupt can be enabled by setting the txie interrupt enable bit of the pie1 register. however, the txif flag bit will be set whenever the txreg is empty, regardless of the state of txie enable bit. to use interrupts when transmitting data, set the txie bit only when there is more data to send. clear the txie interrupt enable bit upon writing the last character of the transmission to the txreg. note 1: when the spen bit is set the rx/dt i/o pin is automatically configured as an input, regardless of the state of the corresponding tris bit and whether or not the eusart receiver is enabled. the rx/dt pin data can be read via a normal port read but port latch data output is precluded. 2: the txif transmitter interrupt flag is set when the txen enable bit is set. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 173 pic18(l)f1xk22 15.1.1.5 tsr status the trmt bit of the txsta register indicates the status of the tsr register. this is a read-only bit. the trmt bit is set when the tsr register is empty and is cleared when a character is transferred to the tsr register from the txreg. the trmt bit remains clear until all bits have been shifted out of the tsr register. no interrupt logic is tied to this bit, so the user needs to poll this bit to determine the tsr status. 15.1.1.6 transmitting 9-bit characters the eusart supports 9-bit character transmissions. when the tx9 bit of the txsta register is set, the eusart will shift 9 bits out for each character transmitted. the tx9d bit of the txsta register is the ninth, and most significant, data bit. when transmitting 9-bit data, the tx9d data bit must be written before writing the 8 least significant bits into the txreg. all nine bits of data will be transferred to the tsr shift register immediately after the txreg is written. a special 9-bit address mode is available for use with multiple receivers. see section 15.1.2.8 ?address detection? for more information on the address mode. 15.1.1.7 asynchronous transmission set-up 1. initialize the spbrgh:spbrg register pair and the brgh and brg16 bits to achieve the desired baud rate (see section 15.3 ?eusart baud rate generator (brg)? ). 2. enable the asynchronous serial port by clearing the sync bit and setting the spen bit. 3. if 9-bit transmission is desired, set the tx9 control bit. a set ninth data bit will indicate that the 8 least significant data bits are an address when the receiver is set for address detection. 4. set the cktxp control bit if inverted transmit data polarity is desired. 5. enable the transmission by setting the txen control bit. this will cause the txif interrupt bit to be set. 6. if interrupts are desired, set the txie interrupt enable bit. an interrupt will occur immediately provided that the gie and peie bits of the intcon register are also set. 7. if 9-bit transmission is selected, the ninth bit should be loaded into the tx9d data bit. 8. load 8-bit data into the txreg register. this will start the transmission. figure 15-3: asynchronous transmission note: the tsr register is not mapped in data memory, so it is not available to the user. word 1 stop bit word 1 transmit shift reg start bit bit 0 bit 1 bit 7/8 write to txreg word 1 brg output (shift clock) rb7/tx/ck txif bit (transmit buffer reg. empty flag) trmt bit (transmit shift reg. empty flag) 1 t cy pin downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 174 ? 2009-2016 microchip technology inc. figure 15-4: asynchronous transmiss ion (back-to-back) table 15-1: registers associated wi th asynchronous transmission name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page baudcon abdovf rcidl dtrxp cktxp brg16 wue abden 247 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr1 adip rcip txip sspip ccp1ip tmr2ip tmr1ip 248 pie1 adie rcie txie sspie ccp1ie tmr2ie tmr1ie 248 pir1 adif rcif txif sspif ccp1if tmr2if tmr1if 248 rcsta spen rx9 sren cren adden ferr oerr rx9d 247 spbrg eusart baud rate generator register, low byte 247 spbrgh eusart baud rate generator register, high byte 247 txreg eusart transmit register 247 txsta csrc tx9 txen sync sendb brgh trmt tx9d 247 legend: = unimplemented locations read as 0 . shaded cells are not used for asynchronous transmission. transmit shift reg write to txreg brg output (shift clock) rb7/tx/ck txif bit (interrupt reg. flag) trmt bit (transmit shift reg. empty flag) word 1 word 2 word 1 word 2 start bit stop bit start bit transmit shift reg word 1 word 2 bit 0 bit 1 bit 7/8 bit 0 note: this timing diagram shows two consecutive transmissions. 1 t cy 1 t cy pin downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 175 pic18(l)f1xk22 15.1.2 eusart asynchronous receiver the asynchronous mode would typically be used in rs-232 systems. the receiver block diagram is shown in figure 15-2 . the data is received on the rx/dt pin and drives the data recovery block. the data recovery block is actually a high-speed shifter operating at 16 times the baud rate, whereas the serial receive shift register (rsr) operates at the bit rate. when all 8 or 9 bits of the character have been shifted in, they are immediately transferred to a two character first-in-first-out (fifo) memory. the fifo buffering allows reception of two complete characters and the start of a third character before software must start servicing the eusart receiver. the fifo and rsr registers are not directly accessible by software. access to the received data is via the rcreg register. 15.1.2.1 enabling the receiver the eusart receiver is enabled for asynchronous operation by configuring the following three control bits: cren = 1 sync = 0 spen = 1 all other eusart control bits are assumed to be in their default state. setting the cren bit of the rcsta register enables the receiver circuitry of the eusart. clearing the sync bit of the txsta register configures the eusart for asynchronous operation. setting the spen bit of the rcsta register enables the eusart. the rx/dt i/o pin must be configured as an input by setting the corresponding tris control bit. if the rx/dt pin is shared with an analog peripheral the analog i/o function must be disabled by clearing the corresponding ansel bit. 15.1.2.2 receiving data the receiver data recovery circuit initiates character reception on the falling edge of the first bit. the first bit, also known as the start bit, is always a zero. the data recovery circuit counts one-half bit time to the center of the start bit and verifies that the bit is still a zero. if it is not a zero then the data recovery circuit aborts character reception, without generating an error, and resumes looking for the falling edge of the start bit. if the start bit zero verification succeeds then the data recovery circuit counts a full bit time to the center of the next bit. the bit is then sampled by a majority detect circuit and the resulting 0 or 1 is shifted into the rsr. this repeats until all data bits have been sampled and shifted into the rsr. one final bit time is measured and the level sampled. this is the stop bit, which is always a 1 . if the data recovery circuit samples a 0 in the stop bit position then a framing error is set for this character, otherwise the framing error is cleared for this character. see section 15.1.2.5 ?receive framing error? for more information on framing errors. immediately after all data bits and the stop bit have been received, the character in the rsr is transferred to the eusart receive fifo and the rcif interrupt flag bit of the pir1 register is set. the top character in the fifo is transferred out of the fifo by reading the rcreg register. 15.1.2.3 receive data polarity the polarity of the receive data can be controlled with the dtrxp bit of the baudcon register. the default state of this bit is 0 which selects high true receive idle and data bits. setting the dtrxp bit to 1 will invert the receive data resulting in low true idle and data bits. the dtrxp bit controls receive data polarity only in asynchronous mode. in synchronous mode the dtrxp bit has a different function. note: when the spen bit is set the tx/ck i/o pin is automatically configured as an output, regardless of the state of the corresponding tris bit and whether or not the eusart transmitter is enabled. the port latch is disconnected from the output driver so it is not possible to use the tx/ck pin as a general purpose output. note: if the receive fifo is overrun, no additional characters will be received until the overrun condition is cleared. see section 15.1.2.6 ?receive overrun error? for more information on overrun errors. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 176 ? 2009-2016 microchip technology inc. 15.1.2.4 receive interrupts the rcif interrupt flag bit of the pir1 register is set whenever the eusart receiver is enabled and there is an unread character in the receive fifo. the rcif interrupt flag bit is read-only, it cannot be set or cleared by software. rcif interrupts are enabled by setting the following bits: rcie interrupt enable bit of the pie1 register peie peripheral interrupt enable bit of the intcon register gie global interrupt enable bit of the intcon register the rcif interrupt flag bit will be set when there is an unread character in the fifo, regardless of the state of interrupt enable bits. 15.1.2.5 receive framing error each character in the receive fifo buffer has a corresponding framing error status bit. a framing error indicates that a stop bit was not seen at the expected time. the framing error status is accessed via the ferr bit of the rcsta register. the ferr bit represents the status of the top unread character in the receive fifo. therefore, the ferr bit must be read before reading the rcreg. the ferr bit is read-only and only applies to the top unread character in the receive fifo. a framing error (ferr = 1 ) does not preclude reception of additional characters. it is not necessary to clear the ferr bit. reading the next character from the fifo buffer will advance the fifo to the next character and the next corresponding framing error. the ferr bit can be forced clear by clearing the spen bit of the rcsta register which resets the eusart. clearing the cren bit of the rcsta register does not affect the ferr bit. a framing error by itself does not generate an interrupt. 15.1.2.6 receive overrun error the receive fifo buffer can hold two characters. an overrun error will be generated if a third character, in its entirety, is received before the fifo is accessed. when this happens the oerr bit of the rcsta register is set. the characters already in the fifo buffer can be read but no additional characters will be received until the error is cleared. the error must be cleared by either clearing the cren bit of the rcsta register or by resetting the eusart by clearing the spen bit of the rcsta register. 15.1.2.7 receiving 9-bit characters the eusart supports 9-bit character reception. when the rx9 bit of the rcsta register is set, the eusart will shift nine bits into the rsr for each character received. the rx9d bit of the rcsta register is the ninth and most significant data bit of the top unread character in the receive fifo. when reading 9-bit data from the receive fifo buffer, the rx9d data bit must be read before reading the eight least significant bits from the rcreg. 15.1.2.8 address detection a special address detection mode is available for use when multiple receivers share the same transmission line, such as in rs-485 systems. address detection is enabled by setting the adden bit of the rcsta register. address detection requires 9-bit character reception. when address detection is enabled, only characters with the ninth data bit set will be transferred to the receive fifo buffer, thereby setting the rcif interrupt bit. all other characters will be ignored. upon receiving an address character, user software determines if the address matches its own. upon address match, user software must disable address detection by clearing the adden bit before the next stop bit occurs. when user software detects the end of the message, determined by the message protocol used, software places the receiver back into the address detection mode by setting the adden bit. note: if all receive characters in the receive fifo have framing errors, repeated reads of the rcreg will not clear the ferr bit. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 177 pic18(l)f1xk22 15.1.2.9 asynchronous reception set-up 1. initialize the spbrgh:spbrg register pair and the brgh and brg16 bits to achieve the desired baud rate (see section 15.3 ?eusart baud rate generator (brg)? ). 2. enable the serial port by setting the spen bit and the rx/dt pin tris bit. the sync bit must be clear for asynchronous operation. 3. if interrupts are desired, set the rcie interrupt enable bit and set the gie and peie bits of the intcon register. 4. if 9-bit reception is desired, set the rx9 bit. 5. set the dtrxp if inverted receive polarity is desired. 6. enable reception by setting the cren bit. 7. the rcif interrupt flag bit will be set when a character is transferred from the rsr to the receive buffer. an interrupt will be generated if the rcie interrupt enable bit was also set. 8. read the rcsta register to get the error flags and, if 9-bit data reception is enabled, the ninth data bit. 9. get the received 8 least significant data bits from the receive buffer by reading the rcreg register. 10. if an overrun occurred, clear the oerr flag by clearing the cren receiver enable bit. 15.1.2.10 9-bit address detection mode set-up this mode would typically be used in rs-485 systems. to set up an asynchronous reception with address detect enable: 1. initialize the spbrgh, spbrg register pair and the brgh and brg16 bits to achieve the desired baud rate (see section 15.3 ?eusart baud rate generator (brg)? ). 2. enable the serial port by setting the spen bit. the sync bit must be clear for asynchronous operation. 3. if interrupts are desired, set the rcie interrupt enable bit and set the gie and peie bits of the intcon register. 4. enable 9-bit reception by setting the rx9 bit. 5. enable address detection by setting the adden bit. 6. set the dtrxp if inverted receive polarity is desired. 7. enable reception by setting the cren bit. 8. the rcif interrupt flag bit will be set when a character with the ninth bit set is transferred from the rsr to the receive buffer. an interrupt will be generated if the rcie interrupt enable bit was also set. 9. read the rcsta register to get the error flags. the ninth data bit will always be set. 10. get the received 8 least significant data bits from the receive buffer by reading the rcreg register. software determines if this is the devices address. 11. if an overrun occurred, clear the oerr flag by clearing the cren receiver enable bit. 12. if the device has been addressed, clear the adden bit to allow all received data into the receive buffer and generate interrupts. figure 15-5: asynchronous reception start bit bit 7/8 bit 1 bit 0 bit 7/8 bit 0 stop bit start bit start bit bit 7/8 stop bit rx/dt pin reg rcv buffer reg rcv shift read rcv buffer reg rcreg rcif (interrupt flag) oerr bit cren word 1 rcreg word 2 rcreg stop bit note: this timing diagram shows three words appearing on the rx input. the rcreg (rece ive buffer) is read after the third word, causing the oerr (overrun) bit to be set. rcidl downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 178 ? 2009-2016 microchip technology inc. table 15-2: registers associated with asynchronous reception name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page baudcon abdovf rcidl dtrxp cktxp brg16 wue abden 247 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr1 adip rcip txip sspip ccp1ip tmr2ip tmr1ip 248 pie1 adie rcie txie sspie ccp1ie tmr2ie tmr1ie 248 pir1 adif rcif txif sspif ccp1if tmr2if tmr1if 248 rcreg eusart receive register 247 rcsta spen rx9 sren cren adden ferr oerr rx9d 247 spbrg eusart baud rate generator register, low byte 247 spbrgh eusart baud rate generator register, high byte 247 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 248 txsta csrc tx9 txen sync sendb brgh trmt tx9d 247 legend: = unimplemented locations read as 0 . shaded cells are not used for asynchronous reception. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 179 pic18(l)f1xk22 15.2 clock accuracy with asynchronous operation the factory calibrates the internal oscillator block output (hfintosc). however, the hfintosc frequency may drift as v dd or temperature changes, and this directly affects the asynchronous baud rate. two methods may be used to adjust the baud rate clock, but both require a reference clock source of some kind. the first (preferred) method uses the osctune register to adjust the hfintosc output. adjusting the value in the osctune register allows for fine resolution changes to the system clock source. see section 2.7.1 ?osctune register? for more information. the other method adjusts the value in the baud rate generator. this can be done automatically with the auto-baud detect feature (see section 15.3.1 ?auto-baud detect? ). there may not be fine enough resolution when adjusting the baud rate generator to compensate for a gradual change in the peripheral clock frequency. register 15-1: txsta: transmit status and control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r-1 r/w-0 csrc tx9 txen (1) sync sendb brgh trmt tx9d bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 csrc: clock source select bit asynchronous mode : dont care synchronous mode : 1 = master mode (clock generated internally from brg) 0 = slave mode (clock from external source) bit 6 tx9: 9-bit transmit enable bit 1 = selects 9-bit transmission 0 = selects 8-bit transmission bit 5 txen: transmit enable bit (1) 1 = transmit enabled 0 = transmit disabled bit 4 sync: eusart mode select bit 1 = synchronous mode 0 = asynchronous mode bit 3 sendb: send break character bit asynchronous mode : 1 = send sync break on next transmission (cleared by hardware upon completion) 0 = sync break transmission completed synchronous mode : dont care bit 2 brgh: high baud rate select bit asynchronous mode : 1 = high speed 0 = low speed synchronous mode: unused in this mode bit 1 trmt: transmit shift register status bit 1 = tsr empty 0 = tsr full bit 0 tx9d: ninth bit of transmit data can be address/data bit or a parity bit. note 1: sren/cren overrides txen in sync mode. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 180 ? 2009-2016 microchip technology inc. register 15-2: rcsta: receive status and control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r-0 r-0 r-x spen rx9 sren cren adden ferr oerr rx9d bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 spen: serial port enable bit 1 = serial port enabled (configures rx/dt and tx/ck pins as serial port pins) 0 = serial port disabled (held in reset) bit 6 rx9: 9-bit receive enable bit 1 = selects 9-bit reception 0 = selects 8-bit reception bit 5 sren: single receive enable bit asynchronous mode : dont care synchronous mode C master : 1 = enables single receive 0 = disables single receive this bit is cleared after reception is complete. synchronous mode C slave dont care bit 4 cren: continuous receive enable bit asynchronous mode : 1 = enables receiver 0 = disables receiver synchronous mode : 1 = enables continuous receive until enable bit cren is cleared (cren overrides sren) 0 = disables continuous receive bit 3 adden: address detect enable bit asynchronous mode 9-bit (rx9 = 1 ) : 1 = enables address detection, enable interrupt and load the receive buffer when rsr<8> is set 0 = disables address detection, all bytes are received and ninth bit can be used as parity bit asynchronous mode 8-bit (rx9 = 0 ) : dont care bit 2 ferr: framing error bit 1 = framing error (can be updated by reading rcreg register and receive next vali d byte) 0 = no framing error bit 1 oerr: overrun error bit 1 = overrun error (can be cleared by clearing bit cren) 0 = no overrun error bit 0 rx9d: ninth bit of received data this can be address/data bit or a parity bit and must be calculated by user firmware. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 181 pic18(l)f1xk22 register 15-3: baudcon: ba ud rate control register r-0 r-1 r/w-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 abdovf rcidl dtrxp cktxp brg16 wue abden bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 abdovf: auto-baud detect overflow bit asynchronous mode : 1 = auto-baud timer overflowed 0 = auto-baud timer did not overflow synchronous mode : dont care bit 6 rcidl : receive idle flag bit asynchronous mode : 1 = receiver is idle 0 = start bit has been detected and the receiver is active synchronous mode : dont care bit 5 dtrxp : data/receive polarity select bit asynchronous mode : 1 = receive data (rx) is inverted (active-low) 0 = receive data (rx) is not inverted (active-high) synchronous mode : 1 = data (dt) is inverted (active-low) 0 = data (dt) is not inverted (active-high) bit 4 cktxp : clock/transmit polarity select bit asynchronous mode : 1 = idle state for transmit (tx) is low 0 = idle state for transmit (tx) is high synchronous mode : 1 = data changes on the falling edge of the clock and is sampled on the rising edge of the clock 0 = data changes on the rising edge of the clock and is sampled on the falling edge of the cl ock bit 3 brg16: 16-bit baud rate generator bit 1 = 16-bit baud rate generator is used (spbrgh:spbrg) 0 = 8-bit baud rate generator is used (spbrg) bit 2 unimplemented: read as 0 bit 1 wue: wake-up enable bit asynchronous mode : 1 = receiver is waiting for a falling edge. no character will be received but rcif will be s et on the falling edge. wue will automatically clear on the rising edge. 0 = receiver is operating normally synchronous mode : dont care bit 0 abden : auto-baud detect enable bit asynchronous mode : 1 = auto-baud detect mode is enabled (clears when auto-baud is complete) 0 = auto-baud detect mode is disabled synchronous mode : dont care downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 182 ? 2009-2016 microchip technology inc. 15.3 eusart baud rate generator (brg) the baud rate generator (brg) is an 8-bit or 16-bit timer that is dedicated to the support of both the asynchronous and synchronous eusart operation. by default, the brg operates in 8-bit mode. setting the brg16 bit of the baudcon register selects 16-bit mode. the spbrgh:spbrg register pair determines the period of the free running baud rate timer. in asynchronous mode the multiplier of the baud rate period is determined by both the brgh bit of the txsta register and the brg16 bit of the baudcon register. in synchronous mode, the brgh bit is ignored. table 15-3 contains the formulas for determining the baud rate. example 15-1 provides a sample calculation for determining the baud rate and baud rate error. typical baud rates and error values for various asynchronous modes have been computed for your convenience and are shown in table 15-5 . it may be advantageous to use the high baud rate (brgh = 1 ), or the 16-bit brg (brg16 = 1 ) to reduce the baud rate error. the 16-bit brg mode is used to achieve slow baud rates for fast oscillator frequencies. writing a new value to the spbrgh, spbrg register pair causes the brg timer to be reset (or cleared). this ensures that the brg does not wait for a timer overflow before outputting the new baud rate. if the system clock is changed during an active receive operation, a receive error or data loss may result. to avoid this problem, check the status of the rcidl bit to make sure that the receive operation is idle before changing the system clock. example 15-1: calculating baud rate error table 15-3: baud rate formulas table 15-4: registers associated with baud rate generator for a device with f osc of 16 mhz, desired baud rate of 9600, asynchronous mode, 8-bit brg: solving for spbrgh:spbrg: desired baud rate f osc 64 [spbrgh:spbrg] 1 + ?? -------------------------------------------------------------------- - = 25.042 ?? 25 == calculated baud rate 16000000 64 25 1+ ?? -------------------------- - = 9615 = error calc. baud rate desired baud rate ? desired baud rate -------------------------------------------------------------------------------------------- = 9615 9600 ? ?? 9600 ---------------------------------- 0 . 1 6 % == f osc x = 64* ( desired baud rate ) -1 ( ) 16,000,000 = 64* 9600 -1 ( ) configuration bits brg/eusart mode baud rate formula sync brg16 brgh 000 8-bit/asynchronous f osc /[64 (n+1)] 001 8-bit/asynchronous f osc /[16 (n+1)] 010 16-bit/asynchronous 011 16-bit/asynchronous f osc /[4 (n+1)] 10x 8-bit/synchronous 11x 16-bit/synchronous legend: x = dont care, n = value of spbrgh, spbrg register pair name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page baudcon abdovf rcidl dtrxp cktxp brg16 wue abden 247 rcsta spen rx9 sren cren adden ferr oerr rx9d 247 spbrg eusart baud rate generator register, low byte 247 spbrgh eusart baud rate generator register, high byte 247 txsta csrc tx9 txen sync sendb brgh trmt tx9d 247 legend: = unimplemented, read as 0 . shaded cells are not used by the brg. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 183 pic18(l)f1xk22 table 15-5: baud rates for asynchronous modes baud rate sync = 0 , brgh = 0 , brg16 = 0 f osc = 48.000 mhz f osc = 18.432 mhz f osc = 12.000 mhz f osc = 11.0592 mhz actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) 3 0 0 1200 1200 0.00 239 1202 0.16 155 1200 0.00 143 2400 2400 0.00 119 2404 0.16 77 2400 0.00 71 9600 9615 0.16 77 9600 0.00 29 9375 -2.34 19 9600 0.00 17 10417 10417 0.00 71 10286 -1.26 27 10417 0.00 17 10165 -2.42 16 19.2k 19.23k 0.16 38 19.20k 0.00 14 18.75k -2.34 9 19.20k 0.00 8 57.6k 57.69k 0.16 12 57.60k 0.00 7 57.60k 0.00 2 115.2k baud rate sync = 0 , brgh = 0 , brg16 = 0 f osc = 8.000 mhz f osc = 4.000 mhz f osc = 3.6864 mhz f osc = 1.000 mhz actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) 300 300 0.16 207 300 0.00 191 300 0.16 51 1200 1202 0.16 103 1202 0.16 51 1200 0.00 47 1202 0.16 12 2400 2404 0.16 51 2404 0.16 25 2400 0.00 23 9600 9615 0.16 12 9600 0.00 5 10417 10417 0.00 11 10417 0.00 5 19.2k 19.20k 0.00 2 57.6k 57.60k 0.00 0 115.2k baud rate sync = 0 , brgh = 1 , brg16 = 0 f osc = 48.000 mhz f osc = 18.432 mhz f osc = 12.000 mhz f osc = 11.0592 mhz actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) 300 1200 2400 9600 9600 0.00 119 9615 0.16 77 9600 0.00 71 10417 10378 -0.37 110 10417 0.00 71 10473 0.53 65 19.2k 19.23k 0.16 155 19.20k 0.00 59 19.23k 0.16 38 19.20k 0.00 35 57.6k 57.69k 0.16 51 57.60k 0.00 19 57.69k 0.16 12 57.60k 0.00 11 115.2k 115.38k 0.16 25 115.2k 0.00 9 115.2k 0.00 5 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 184 ? 2009-2016 microchip technology inc. baud rate sync = 0 , brgh = 1 , brg16 = 0 f osc = 8.000 mhz f osc = 4.000 mhz f osc = 3.6864 mhz f osc = 1.000 mhz actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) 300 300 0.16 207 1200 1202 0.16 207 1200 0.00 191 1202 0.16 51 2400 2404 0.16 207 2404 0.16 103 2400 0.00 95 2404 0.16 25 9600 9615 0.16 51 9615 0.16 25 9600 0.00 23 10417 10417 0.00 47 10417 0.00 23 10473 0.53 21 10417 0.00 5 19.2k 19231 0.16 25 19.23k 0.16 12 19.2k 0.00 11 57.6k 55556 -3.55 8 57.60k 0.00 3 115.2k 115.2k 0.00 1 baud rate sync = 0 , brgh = 0 , brg16 = 1 f osc = 48.000 mhz f osc = 18.432 mhz f osc = 12.000 mhz f osc = 11.0592 mhz actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) 300 300.0 0.00 9999 300.0 0.00 3839 300 0.00 2499 300.0 0.00 2303 1200 1200.1 0.00 2499 1200 0.00 959 1200 0.00 624 1200 0.00 575 2400 2400 0.00 1249 2400 0.00 479 2404 0.16 311 2400 0.00 287 9600 9615 0.16 311 9600 0.00 119 9615 0.16 77 9600 0.00 71 10417 10417 0.00 287 10378 -0.37 110 10417 0.00 71 10473 0.53 65 19.2k 19.23k 0.16 155 19.20k 0.00 59 19.23k 0.16 38 19.20k 0.00 35 57.6k 57.69k 0.16 51 57.60k 0.00 19 57.69k 0.16 12 57.60k 0.00 11 115.2k 115.38k 0.16 25 115.2k 0.00 9 115.2k 0.00 5 baud rate sync = 0 , brgh = 0 , brg16 = 1 f osc = 8.000 mhz f osc = 4.000 mhz f osc = 3.6864 mhz f osc = 1.000 mhz actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) 300 299.9 -0.02 1666 300.1 0.04 832 300.0 0.00 767 300.5 0.16 207 1200 1199 -0.08 416 1202 0.16 207 1200 0.00 191 1202 0.16 51 2400 2404 0.16 207 2404 0.16 103 2400 0.00 95 2404 0.16 25 9600 9615 0.16 51 9615 0.16 25 9600 0.00 23 10417 10417 0.00 47 10417 0.00 23 10473 0.53 21 10417 0.00 5 19.2k 19.23k 0.16 25 19.23k 0.16 12 19.20k 0.00 11 57.6k 55556 -3.55 8 57.60k 0.00 3 115.2k 115.2k 0.00 1 table 15-5: baud rates for asynchronous modes (continued) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 185 pic18(l)f1xk22 baud rate sync = 0 , brgh = 1 , brg16 = 1 or sync = 1 , brg16 = 1 f osc = 48.000 mhz f osc = 18.432 mhz f osc = 12.000 mhz f osc = 11.0592 mhz actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) 300 300 0.00 39999 300.0 0.00 15359 300 0.00 9999 300.0 0.00 9215 1200 1200 0.00 9999 1200 0.00 3839 1200 0.00 2499 1200 0.00 2303 2400 2400 0.00 4999 2400 0.00 1919 2400 0.00 1249 2400 0.00 1151 9600 9600 0.00 1249 9600 0.00 479 9615 0.16 311 9600 0.00 287 10417 10417 0.00 1151 10425 0.08 441 10417 0.00 287 10433 0.16 264 19.2k 19.20k 0.00 624 19.20k 0.00 239 19.23k 0.16 155 19.20k 0.00 143 57.6k 57.69k 0.16 207 57.60k 0.00 79 57.69k 0.16 51 57.60k 0.00 47 115.2k 115.38k 0.16 103 115.2k 0.00 39 115.38k 0.16 25 115.2k 0.00 23 baud rate sync = 0 , brgh = 1 , brg16 = 1 or sync = 1 , brg16 = 1 f osc = 8.000 mhz f osc = 4.000 mhz f osc = 3.6864 mhz f osc = 1.000 mhz actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) 300 300.0 0.00 6666 300.0 0.01 3332 300.0 0.00 3071 300.1 0.04 832 1200 1200 -0.02 1666 1200 0.04 832 1200 0.00 767 1202 0.16 207 2400 2401 0.04 832 2398 0.08 416 2400 0.00 383 2404 0.16 103 9600 9615 0.16 207 9615 0.16 103 9600 0.00 95 9615 0.16 25 10417 10417 0.00 191 10417 0.00 95 10473 0.53 87 10417 0.00 23 19.2k 19.23k 0.16 103 19.23k 0.16 51 19.20k 0.00 47 19.23k 0.16 12 57.6k 57.14k -0.79 34 58.82k 2.12 16 57.60k 0.00 15 115.2k 117.6k 2.12 16 111.1k -3.55 8 115.2k 0.00 7 table 15-5: baud rates for asynchronous modes (continued) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 186 ? 2009-2016 microchip technology inc. 15.3.1 auto-baud detect the eusart module supports automatic detection and calibration of the baud rate. in the auto-baud detect (abd) mode, the clock to the brg is reversed. rather than the brg clocking the incoming rx signal, the rx signal is timing the brg. the baud rate generator is used to time the period of a received 55h (ascii u), which is the sync character for the lin bus. the unique feature of this character is that it has five rising edges including the stop bit edge. setting the abden bit of the baudcon register starts the auto-baud calibration sequence ( figure 15-6 ). while the abd sequence takes place, the eusart state machine is held in idle. on the first rising edge of the receive line, after the start bit, the spbrg begins counting up using the brg counter clock as shown in table 15-6 . the fifth rising edge will occur on the rx pin at the end of the eighth bit period. at that time, an accumulated value totaling the proper brg period is left in the spbrgh:spbrg register pair, the abden bit is automatically cleared, and the rcif interrupt flag is set. a read operation on the rcreg needs to be performed to clear the rcif interrupt. rcreg content should be discarded. when calibrating for modes that do not use the spbrgh register the user can verify that the spbrg register did not overflow by checking for 00h in the spbrgh register. the brg auto-baud clock is determined by the brg16 and brgh bits as shown in table 15-6 . during abd, both the spbrgh and spbrg registers are used as a 16-bit counter, independent of the brg16 bit setting. while calibrating the baud rate period, the spbrgh and spbrg registers are clocked at 1/8th the brg base clock rate. the resulting byte measurement is the average bit time when clocked at full speed. table 15-6: brg counter clock rates figure 15-6: automatic baud rate calibration note 1: if the wue bit is set with the abden bit, auto-baud detection will occur on the byte following the break character (see section 15.3.3 ?auto-wake-up on break? ). 2: it is up to the user to determine that the incoming character baud rate is within the range of the selected brg clock source. some combinations of oscillator frequency and eusart baud rates are not possible. 3: during the auto-baud process, the auto-baud counter starts counting at 1. upon completion of the auto-baud sequence, to achieve maximum accuracy, subtract 1 from the spbrgh:spbrg register pair. brg16 brgh brg base clock brg abd clock 00 f osc /64 f osc /512 01 f osc /16 f osc /128 10 f osc /16 f osc /128 11 f osc /4 f osc /32 note: during the abd sequence, spbrg and spbrgh registers are both used as a 16-bit counter, independent of brg16 setting. brg value rx pin abden bit rcif bit bit 0 bit 1 (interrupt) read rcreg brg clock start auto cleared set by user xxxxh 0000h edge #1 bit 2 bit 3 edge #2 bit 4 bit 5 edge #3 bit 6 bit 7 edge #4 stop bit edge #5 001ch note 1: the abd sequence requires the eusart module to be configured in asynchronous mode. spbrg xxh 1ch spbrgh xxh 00h rcidl downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 187 pic18(l)f1xk22 15.3.2 auto-baud overflow during the course of automatic baud detection, the abdovf bit of the baudcon register will be set if the baud rate counter overflows before the fifth rising edge is detected on the rx pin. the abdovf bit indicates that the counter has exceeded the maximum count that can fit in the 16 bits of the spbrgh:spbrg register pair. after the abdovf has been set, the counter continues to count until the fifth rising edge is detected on the rx pin. upon detecting the fifth rx edge, the hardware will set the rcif interrupt flag and clear the abden bit of the baudcon register. the rcif flag can be subsequently cleared by reading the rcreg register. the abdovf flag of the baudcon register can be cleared by software directly. to terminate the auto-baud process before the rcif flag is set, clear the abden bit then clear the abdovf bit of the baudcon register. the abdovf bit will remain set if the abden bit is not cleared first. 15.3.3 auto-wake-up on break during sleep mode, all clocks to the eusart are suspended. because of this, the baud rate generator is inactive and a proper character reception cannot be performed. the auto-wake-up feature allows the controller to wake-up due to activity on the rx/dt line. this feature is available only in asynchronous mode. the auto-wake-up feature is enabled by setting the wue bit of the baudcon register. once set, the normal receive sequence on rx/dt is disabled, and the eusart remains in an idle state, monitoring for a wake-up event independent of the cpu mode. a wake-up event consists of a high-to-low transition on the rx/dt line. (this coincides with the start of a sync break or a wake-up signal character for the lin protocol.) the eusart module generates an rcif interrupt coincident with the wake-up event. the interrupt is generated synchronously to the q clocks in normal cpu operating modes ( figure 15-7 ), and asynchronously if the device is in sleep mode ( figure 15-8 ). the interrupt condition is cleared by reading the rcreg register. the wue bit is automatically cleared by the low-to-high transition on the rx line at the end of the break. this signals to the user that the break event is over. at this point, the eusart module is in idle mode waiting to receive the next character. 15.3.3.1 special considerations break character to avoid character errors or character fragments during a wake-up event, the wake-up character must be all zeros. when the wake-up is enabled the function works independent of the low time on the data stream. if the wue bit is set and a valid non-zero character is received, the low time from the start bit to the first rising edge will be interpreted as the wake-up event. the remaining bits in the character will be received as a fragmented character and subsequent characters can result in framing or overrun errors. therefore, the initial character in the transmission must be all 0 s. this must be 10 or more bit times, 13-bit times recommended for lin bus, or any number of bit times for standard rs-232 devices. oscillator start-up time oscillator start-up time must be considered, especially in applications using oscillators with longer start-up intervals (i.e., lp, xt or hs/pll mode). the sync break (or wake-up signal) character must be of sufficient length, and be followed by a sufficient interval, to allow enough time for the selected oscillator to start and provide proper initialization of the eusart. wue bit the wake-up event causes a receive interrupt by setting the rcif bit. the wue bit is cleared by hardware by a rising edge on rx/dt. the interrupt condition is then cleared by software by reading the rcreg register and discarding its contents. to ensure that no actual data is lost, check the rcidl bit to verify that a receive operation is not in process before setting the wue bit. if a receive operation is not occurring, the wue bit may then be set just prior to entering the sleep mode. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 188 ? 2009-2016 microchip technology inc. figure 15-7: auto-wake-up bit (wue) timing during normal operation figure 15-8: auto-wake-up bit (wue) timings during sleep q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 osc1 wue bit rx/dt line rcif bit set by user auto cleared cleared due to user read of rcreg note 1: the eusart remains in idle while the wue bit is set. q1 q2 q3 q4 q1q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 osc1 wue bit rx/dt line rcif bit set by user auto cleared cleared due to user read of rcreg sleep command executed note 1 note 1: if the wake-up event requires long oscillator warm-up time, the automatic clearin g of the wue bit can occur while the stposc signal is still active. this sequence should not depend on the presence of q clocks. 2: the eusart remains in idle while the wue bit is set. sleep ends downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 189 pic18(l)f1xk22 15.3.4 break character sequence the eusart module has the capability of sending the special break character sequences that are required by the lin bus standard. a break character consists of a start bit, followed by 12 0 bits and a stop bit. to send a break character, set the sendb and txen bits of the txsta register. the break character transmission is then initiated by a write to the txreg. the value of data written to txreg will be ignored and all 0 s will be transmitted. the sendb bit is automatically reset by hardware after the corresponding stop bit is sent. this allows the user to preload the transmit fifo with the next transmit byte following the break character (typically, the sync character in the lin specification). the trmt bit of the txsta register indicates when the transmit operation is active or idle, just as it does during normal transmission. see figure 15-9 for the timing of the break character sequence. 15.3.4.1 break and sync transmit sequence the following sequence will start a message frame header made up of a break, followed by an auto-baud sync byte. this sequence is typical of a lin bus master. 1. configure the eusart for the desired mode. 2. set the txen and sendb bits to enable the break sequence. 3. load the txreg with a dummy character to initiate transmission (the value is ignored). 4. write 55h to txreg to load the sync character into the transmit fifo buffer. 5. after the break has been sent, the sendb bit is reset by hardware and the sync character is then transmitted. when the txreg becomes empty, as indicated by the txif, the next data byte can be written to txreg. 15.3.5 receiving a break character the enhanced eusart module can receive a break character in two ways. the first method to detect a break character uses the ferr bit of the rcsta register and the received data as indicated by rcreg. the baud rate generator is assumed to have been initialized to the expected baud rate. a break character has been received when; rcif bit is set ferr bit is set rcreg = 00h the second method uses the auto-wake-up feature described in section 15.3.3 ?auto-wake-up on break? . by enabling this feature, the eusart will sample the next two transitions on rx/dt, cause an rcif interrupt, and receive the next data byte followed by another interrupt. note that following a break character, the user will typically want to enable the auto-baud detect feature. for both methods, the user can set the abden bit of the baudcon register before placing the eusart in sleep mode. figure 15-9: send break character sequence write to txreg dummy write brg output (shift clock) start bit bit 0 bit 1 bit 11 stop bit break txif bit (transmit interrupt flag) tx (pin) trmt bit (transmit shift reg. empty flag) sendb (send break control bit) sendb sampled here auto cleared downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 190 ? 2009-2016 microchip technology inc. 15.4 eusart synchronous mode synchronous serial communications are typically used in systems with a single master and one or more slaves. the master device contains the necessary circuitry for baud rate generation and supplies the clock for all devices in the system. slave devices can take advantage of the master clock by eliminating the internal clock generation circuitry. there are two signal lines in synchronous mode: a bidirectional data line and a clock line. slaves use the external clock supplied by the master to shift the serial data into and out of their respective receive and transmit shift registers. since the data line is bidirectional, synchronous operation is half-duplex only. half-duplex refers to the fact that master and slave devices can receive and transmit data but not both simultaneously. the eusart can operate as either a master or slave device. start and stop bits are not used in synchronous transmissions. 15.4.1 synchronous master mode the following bits are used to configure the eusart for synchronous master operation: sync = 1 csrc = 1 sren = 0 (for transmit); sren = 1 (for receive) cren = 0 (for transmit); cren = 1 (for receive) spen = 1 setting the sync bit of the txsta register configures the device for synchronous operation. setting the csrc bit of the txsta register configures the device as a master. clearing the sren and cren bits of the rcsta register ensures that the device is in the transmit mode, otherwise the device will be configured to receive. setting the spen bit of the rcsta register enables the eusart. if the rx/dt or tx/ck pins are shared with an analog peripheral the analog i/o functions must be disabled by clearing the corresponding ansel bits. the tris bits corresponding to the rx/dt and tx/ck pins should be set. 15.4.1.1 master clock synchronous data transfers use a separate clock line, which is synchronous with the data. a device configured as a master transmits the clock on the tx/ck line. the tx/ck pin output driver is automatically enabled when the eusart is configured for synchronous transmit or receive operation. serial data bits change on the leading edge to ensure they are valid at the trailing edge of each clock. one clock cycle is generated for each data bit. only as many clock cycles are generated as there are data bits. 15.4.1.2 clock polarity a clock polarity option is provided for microwire compatibility. clock polarity is selected with the cktxp bit of the baudcon register. setting the cktxp bit sets the clock idle state as high. when the cktxp bit is set, the data changes on the falling edge of each clock and is sampled on the rising edge of each clock. clearing the cktxp bit sets the idle state as low. when the cktxp bit is cleared, the data changes on the rising edge of each clock and is sampled on the falling edge of each clock. 15.4.1.3 synchronous master transmission data is transferred out of the device on the rx/dt pin. the rx/dt and tx/ck pin output drivers are automatically enabled when the eusart is configured for synchronous master transmit operation. a transmission is initiated by writing a character to the txreg register. if the tsr still contains all or part of a previous character the new character data is held in the txreg until the last bit of the previous character has been transmitted. if this is the first character, or the previous character has been completely flushed from the tsr, the data in the txreg is immediately transferred to the tsr. the transmission of the character commences immediately following the transfer of the data to the tsr from the txreg. each data bit changes on the leading edge of the master clock and remains valid until the subsequent leading clock edge. 15.4.1.4 data polarity the polarity of the transmit and receive data can be controlled with the dtrxp bit of the baudcon register. the default state of this bit is 0 which selects high true transmit and receive data. setting the dtrxp bit to 1 will invert the data resulting in low true transmit and receive data. note: the tsr register is not mapped in data memory, so it is not available to the user. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 191 pic18(l)f1xk22 15.4.1.5 synchronous master transmission set-up 1. initialize the spbrgh, spbrg register pair and the brgh and brg16 bits to achieve the desired baud rate (see section 15.3 ?eusart baud rate generator (brg)? ). 2. enable the synchronous master serial port by setting bits sync, spen and csrc. set the tris bits corresponding to the rx/dt and tx/ck i/o pins. 3. disable receive mode by clearing bits sren and cren. 4. enable transmit mode by setting the txen bit. 5. if 9-bit transmission is desired, set the tx9 bit. 6. if interrupts are desired, set the txie, gie and peie interrupt enable bits. 7. if 9-bit transmission is selected, the ninth bit should be loaded in the tx9d bit. 8. start transmission by loading data to the txreg register. figure 15-10: synchronous transmission figure 15-11: synchronous transmis sion (through txen) bit 0 bit 1 bit 7 word 1 bit 2 bit 0 bit 1 bit 7 rx/dt write to txreg reg txif bit (interrupt flag) txen bit 1 1 word 2 trmt bit write word 1 write word 2 note: sync master mode, spbrg = 0 , continuous transmission of two 8-bit words. pin tx/ck pin tx/ck pin (sckp = 0 ) (sckp = 1 ) rx/dt pin tx/ck pin write to txreg reg txif bit trmt bit bit 0 bit 1 bit 2 bit 6 bit 7 txen bit downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 192 ? 2009-2016 microchip technology inc. table 15-7: registers associated with synchronous master transmission 15.4.1.6 synchronous master reception data is received at the rx/dt pin. the rx/dt pin output driver must be disabled by setting the corresponding tris bits when the eusart is configured for synchronous master receive operation. in synchronous mode, reception is enabled by setting either the single receive enable bit (sren of the rcsta register) or the continuous receive enable bit (cren of the rcsta register). when sren is set and cren is clear, only as many clock cycles are generated as there are data bits in a single character. the sren bit is automatically cleared at the completion of one character. when cren is set, clocks are continuously generated until cren is cleared. if cren is cleared in the middle of a character the ck clock stops immediately and the partial character is discarded. if sren and cren are both set, then sren is cleared at the completion of the first character and cren takes precedence. to initiate reception, set either sren or cren. data is sampled at the rx/dt pin on the trailing edge of the tx/ck clock pin and is shifted into the receive shift register (rsr). when a complete character is received into the rsr, the rcif bit is set and the character is automatically transferred to the two character receive fifo. the least significant eight bits of the top character in the receive fifo are available in rcreg. the rcif bit remains set as long as there are unread characters in the receive fifo. 15.4.1.7 slave clock synchronous data transfers use a separate clock line, which is synchronous with the data. a device configured as a slave receives the clock on the tx/ck line. the tx/ck pin output driver must be disabled by setting the associated tris bit when the device is configured for synchronous slave transmit or receive operation. serial data bits change on the leading edge to ensure they are valid at the trailing edge of each clock. one data bit is transferred for each clock cycle. only as many clock cycles should be received as there are data bits. 15.4.1.8 receive overrun error the receive fifo buffer can hold two characters. an overrun error will be generated if a third character, in its entirety, is received before rcreg is read to access the fifo. when this happens the oerr bit of the rcsta register is set. previous data in the fifo will not be overwritten. the two characters in the fifo buffer can be read, however, no additional characters will be received until the error is cleared. the oerr bit can only be cleared by clearing the overrun condition. if the overrun error occurred when the sren bit is set and cren is clear then the error is cleared by reading rcreg. if the overrun occurred when the cren bit is set then the error condition is cleared by either clearing the cren bit of the rcsta register or by clearing the spen bit which resets the eusart. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page baudcon abdovf rcidl dtrxp cktxp brg16 wue abden 247 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr1 adip rcip txip sspip ccp1ip tmr2ip tmr1ip 248 pie1 adie rcie txie sspie ccp1ie tmr2ie tmr1ie 248 pir1 adif rcif txif sspif ccp1if tmr2if tmr1if 248 rcsta spen rx9 sren cren adden ferr oerr rx9d 247 spbrg eusart baud rate generator register, low byte 247 spbrgh eusart baud rate generator register, high byte 247 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 248 txreg eusart transmit register 247 txsta csrc tx9 txen sync sendb brgh trmt tx9d 247 legend: = unimplemented, read as 0 . shaded cells are not used for synchronous master transmission. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 193 pic18(l)f1xk22 15.4.1.9 receiving 9-bit characters the eusart supports 9-bit character reception. when the rx9 bit of the rcsta register is set the eusart will shift 9-bits into the rsr for each character received. the rx9d bit of the rcsta register is the ninth, and most significant, data bit of the top unread character in the receive fifo. when reading 9-bit data from the receive fifo buffer, the rx9d data bit must be read before reading the eight least significant bits from the rcreg. 15.4.1.10 synchronous master reception set-up 1. initialize the spbrgh, spbrg register pair for the appropriate baud rate. set or clear the brgh and brg16 bits, as required, to achieve the desired baud rate. 2. enable the synchronous master serial port by setting bits sync, spen and csrc. disable rx/dt and tx/ck output drivers by setting the corresponding tris bits. 3. ensure bits cren and sren are clear. 4. if using interrupts, set the gie and peie bits of the intcon register and set rcie. 5. if 9-bit reception is desired, set bit rx9. 6. start reception by setting the sren bit or for continuous reception, set the cren bit. 7. interrupt flag bit rcif will be set when reception of a character is complete. an interrupt will be generated if the enable bit rcie was set. 8. read the rcsta register to get the ninth bit (if enabled) and determine if any error occurred during reception. 9. read the 8-bit received data by reading the rcreg register. 10. if an overrun error occurs, clear the error by either clearing the cren bit of the rcsta register or by clearing the spen bit which resets the eusart. figure 15-12: synchronous reception (master mode, sren) cren bit rx/dt write to bit sren sren bit rcif bit (interrupt) read rxreg 0 bit 0 bit 1 bit 2 bit 3 bit 4 bit 5 bit 6 bit 7 0 note: timing diagram demonstrates sync master mode with bit sren = 1 and bit brgh = 0 . tx/ck pin tx/ck pin pin (sckp = 0 ) (sckp = 1 ) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 194 ? 2009-2016 microchip technology inc. table 15-8: registers associated with synchronous master reception name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page baudcon abdovf rcidl dtrxp cktxp brg16 wue abden 247 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr1 adip rcip txip sspip ccp1ip tmr2ip tmr1ip 248 pie1 adie rcie txie sspie ccp1ie tmr2ie tmr1ie 248 pir1 adif rcif txif sspif ccp1if tmr2if tmr1if 248 rcreg eusart receive register 247 rcsta spen rx9 sren cren adden ferr oerr rx9d 247 spbrg eusart baud rate generator register, low byte 247 spbrgh eusart baud rate generator register, high byte 247 txsta csrc tx9 txen sync sendb brgh trmt tx9d 247 legend: = unimplemented, read as 0 . shaded cells are not used for synchronous master reception. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 195 pic18(l)f1xk22 15.4.2 synchronous slave mode the following bits are used to configure the eusart for synchronous slave operation: sync = 1 csrc = 0 sren = 0 (for transmit); sren = 1 (for receive) cren = 0 (for transmit); cren = 1 (for receive) spen = 1 setting the sync bit of the txsta register configures the device for synchronous operation. clearing the csrc bit of the txsta register configures the device as a slave. clearing the sren and cren bits of the rcsta register ensures that the device is in the transmit mode, otherwise the device will be configured to receive. setting the spen bit of the rcsta register enables the eusart. if the rx/dt or tx/ck pins are shared with an analog peripheral the analog i/o functions must be disabled by clearing the corresponding ansel bits. rx/dt and tx/ck pin output drivers must be disabled by setting the corresponding tris bits. 15.4.2.1 eusart synchronous slave transmit the operation of the synchronous master and slave modes are identical (see section 15.4.1.3 ?synchronous master transmission? ) , except in the case of the sleep mode. if two words are written to the txreg and then the sleep instruction is executed, the following will occur: 1. the first character will immediately transfer to the tsr register and transmit. 2. the second word will remain in txreg register. 3. the txif bit will not be set. 4. after the first character has been shifted out of tsr, the txreg register will transfer the second character to the tsr and the txif bit will now be set. 5. if the peie and txie bits are set, the interrupt will wake the device from sleep and execute the next instruction. if the gie bit is also set, the program will call the interrupt service routine. 15.4.2.2 synchronous slave transmission set-up 1. set the sync and spen bits and clear the csrc bit. set the tris bits corresponding to the rx/dt and tx/ck i/o pins. 2. clear the cren and sren bits. 3. if using interrupts, ensure that the gie and peie bits of the intcon register are set and set the txie bit. 4. if 9-bit transmission is desired, set the tx9 bit. 5. enable transmission by setting the txen bit. 6. if 9-bit transmission is selected, insert the most significant bit into the tx9d bit. 7. start transmission by writing the least significant eight bits to the txreg register. table 15-9: registers associated with synchronous slave transmission name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page baudcon abdovf rcidl dtrxp cktxp brg16 wue abden 247 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr1 adip rcip txip sspip ccp1ip tmr2ip tmr1ip 248 pie1 adie rcie txie sspie ccp1ie tmr2ie tmr1ie 248 pir1 adif rcif txif sspif ccp1if tmr2if tmr1if 248 rcsta spen rx9 sren cren adden ferr oerr rx9d 247 spbrg eusart baud rate generator register, low byte 247 spbrgh eusart baud rate generator register, high byte 247 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 248 txreg eusart transmit register 247 txsta csrc tx9 txen sync sendb brgh trmt tx9d 247 legend: = unimplemented, read as 0 . shaded cells are not used for synchronous master transmission. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 196 ? 2009-2016 microchip technology inc. 15.4.2.3 eusart synchronous slave reception the operation of the synchronous master and slave modes is identical ( section 15.4.1.6 ?synchronous master reception? ), with the following exceptions: sleep cren bit is always set, therefore the receiver is never idle sren bit, which is a don't care in slave mode a character may be received while in sleep mode by setting the cren bit prior to entering sleep. once the word is received, the rsr register will transfer the data to the rcreg register. if the rcie enable bit is set, the interrupt generated will wake the device from sleep and execute the next instruction. if the gie bit is also set, the program will branch to the interrupt vector. 15.4.2.4 synchronous slave reception set-up 1. set the sync and spen bits and clear the csrc bit. set the tris bits corresponding to the rx/dt and tx/ck i/o pins. 2. if using interrupts, ensure that the gie and peie bits of the intcon register are set and set the rcie bit. 3. if 9-bit reception is desired, set the rx9 bit. 4. set the cren bit to enable reception. 5. the rcif bit will be set when reception is complete. an interrupt will be generated if the rcie bit was set. 6. if 9-bit mode is enabled, retrieve the most significant bit from the rx9d bit of the rcsta register. 7. retrieve the eight least significant bits from the receive fifo by reading the rcreg register. 8. if an overrun error occurs, clear the error by either clearing the cren bit of the rcsta register or by clearing the spen bit which resets the eusart. table 15-10: registers associated wi th synchronous slave reception name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page baudcon abdovf rcidl dtrxp cktxp brg16 wue abden 247 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr1 adip rcip txip sspip ccp1ip tmr2ip tmr1ip 248 pie1 adie rcie txie sspie ccp1ie tmr2ie tmr1ie 248 pir1 adif rcif txif sspif ccp1if tmr2if tmr1if 248 rcreg eusart receive register 247 rcsta spen rx9 sren cren adden ferr oerr rx9d 247 spbrg eusart baud rate generator register, low byte 247 spbrgh eusart baud rate generator register, high byte 247 txsta csrc tx9 txen sync sendb brgh trmt tx9d 247 legend: = unimplemented, read as 0 . shaded cells are not used for synchronous slave reception. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 197 pic18(l)f1xk22 16.0 analog-to-digital converter (adc) module the analog-to-digital converter (adc) allows conversion of an analog input signal to a 10-bit binary representation of that signal. this device uses analog inputs, which are multiplexed into a single sample and hold circuit. the output of the sample and hold is connected to the input of the converter. the converter generates a 10-bit binary result via successive approximation and stores the conversion result into the adc result registers (adresl and adresh). the adc voltage reference is software selectable to either v dd , or a voltage applied to the external reference pins. the adc can generate an interrupt upon completion of a conversion. this interrupt can be used to wake-up the device from sleep. figure 16-1 shows the block diagram of the adc. figure 16-1: adc block diagram adc an4 av dd v ref + adon go/done chs<3:0> adresh adresl 10 10 adfm v ss an5 an6 an7 an3 an8 an9 an10 an11 av ss v ref - nvcfg[1:0] = 00 fvr 00000001 0010 0011 0100 0101 0111 01101000 1001 1010 1011 1100 1101 1110 1111 unusedunused 0 = left justify 1 = right justify dac nvcfg[1:0] = 01 fvr pvcfg[1:0] = 00 pvcfg[1:0] = 01 pvcfg[1:0] = 10 an1 an2 an0 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 198 ? 2009-2016 microchip technology inc. 16.1 adc configuration when configuring and using the adc the following functions must be considered: port configuration channel selection adc voltage reference selection adc conversion clock source interrupt control results formatting 16.1.1 port configuration the ansel, anselh, trisa, trisb and trise registers all configure the a/d port pins. any port pin needed as an analog input should have its corresponding ansx bit set to disable the digital input buffer and trisx bit set to disable the digital output driver. if the trisx bit is cleared, the digital output level (v oh or v ol ) will be converted. the a/d operation is independent of the state of the ansx bits and the tris bits. 16.1.2 channel selection the chs bits of the adcon0 register determine which channel is connected to the sample and hold circuit. when changing channels, a delay is required before starting the next conversion. refer to section 16.2 ?adc operation? for more information. 16.1.3 adc v oltage reference the pvcfg and nvcfg bits of the adcon1 register provide independent control of the positive and negative voltage references, respectively. the positive voltage reference can be either v dd , fvr or an external voltage source. the negative voltage reference can be either v ss or an external voltage source. 16.1.4 selecting and configuring acquisition time the adcon2 register allows the user to select an acquisition time that occurs each time the go/done bit is set. acquisition time is set with the acqt<2:0> bits of the adcon2 register. acquisition delays cover a range of 2 to 20 t ad . when the go/done bit is set, the a/d module continues to sample the input for the selected acquisition time, then automatically begins a conversion. since the acquisition time is programmed, there is no need to wait for an acquisition time between selecting a channel and setting the go/done bit. manual acquisition is selected when acqt<2:0> = 000 . when the go/done bit is set, sampling is stopped and a conversion begins. the user is responsible for ensuring the required acquisition time has passed between selecting the desired input channel and setting the go/done bit. this option is also the default reset state of the acqt<2:0> bits and is compatible with devices that do not offer programmable acquisition times. in either case, when the conversion is completed, the go/done bit is cleared, the adif flag is set and the a/d begins sampling the currently selected channel again. when an acquisition time is programmed, there is no indication of when the acquisition time ends and the conversion begins. 16.1.5 conversion clock the source of the conversion clock is software selectable via the adcs bits of the adcon2 register. there are seven possible clock options: f osc /2 f osc /4 f osc /8 f osc /16 f osc /32 f osc /64 f rc (dedicated internal oscillator) the time to complete one bit conversion is defined as t ad . one full 10-bit conversion requires 11 t ad periods as shown in figure 16-3 . for correct conversion, the appropriate t ad specification must be met. see a/d conversion requirements in table 26-20 for more information. table 16-1 gives examples of appropriate adc clock selections. note 1: when reading the port register, all pins with their corresponding ansx bit set read as cleared (a low level). however, analog conversion of pins configured as digital inputs (ansx bit cleared and trisx bit set) will be accurately converted. 2: analog levels on any pin with the corresponding ansx bit cleared may cause the digital input buffer to consume current out of the devices specification limits. note: unless using the f rc , any changes in the system clock frequency will change the adc clock frequency, which may adversely affect the adc result. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 199 pic18(l)f1xk22 16.1.6 interrupts the adc module allows for the ability to generate an interrupt upon completion of an analog-to-digital conversion. the adc interrupt flag is the adif bit in the pir1 register. the adc interrupt enable is the adie bit in the pie1 register. the adif bit must be cleared by software. this interrupt can be generated while the device is operating or while in sleep. if the device is in sleep, the interrupt will wake-up the device. upon waking from sleep, the next instruction following the sleep instruction is always executed. if the user is attempting to wake-up from sleep and resume in-line code execution, the global interrupt must be disabled. if the global interrupt is enabled, execution will switch to the interrupt service routine. please see section 16.1.6 ?interrupts? for more information. table 16-1: adc clock period (t ad ) vs. device operat ing frequencies 16.1.7 result formatting the 10-bit a/d conversion result can be supplied in two formats, left justified or right justified. the adfm bit of the adcon2 register controls the output format. figure 16-2 shows the two output formats. figure 16-2: 10-bit a/d conv ersion result format note: the adif bit is set at the completion of every conversion, regardless of whether or not the adc interrupt is enabled. adc clock period (t ad ) device frequency (f osc ) adc clock source adcs<2:0> 48 mhz 16 mhz 4 mhz 1 mhz f osc /2 000 41.67 ns (2) 125 ns (2) 500 ns (2) 2.0 ? s f osc /4 100 83.33 ns (2) 250 ns (2) 1.0 ? s4 . 0 ? s f osc /8 001 167 ns (2) 500 ns (2) 2.0 ? s 8.0 ? s (3) f osc /16 101 333 ns (2) 1.0 ? s4 . 0 ? s 16.0 ? s (3) f osc /32 010 667 ns (2) 2.0 ? s 8.0 ? s (3) 32.0 ? s (3) f osc /64 110 1.33 ? s4 . 0 ? s 16.0 ? s (3) 64.0 ? s (3) f rc x11 1-4 ? s (1,4) 1-4 ? s (1,4) 1-4 ? s (1,4) 1-4 ? s (1,4) legend: shaded cells are outside of recommended range. note 1: the f rc source has a typical t ad time of 1.7 ? s. 2: these values violate the minimum required t ad time. 3: for faster conversion times, the selection of another clock source is recommended. 4: when the device frequency is greater than 1 mhz, the f rc clock source is only recommended if the conversion will be performed during sleep. adresh adresl (adfm = 0 )m s b l s b bit 7 bit 0 bit 7 bit 0 10-bit a/d result unimplemented: read as 0 (adfm = 1 ) msb lsb bit 7 bit 0 bit 7 bit 0 unimplemented: read as 0 10-bit a/d result downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 200 ? 2009-2016 microchip technology inc. 16.2 adc operation 16.2.1 starting a conversion to enable the adc module, the adon bit of the adcon0 register must be set to a 1 . setting the go/ done bit of the adcon0 register to a 1 will, depending on the acqt bits of the adcon2 register, either immediately start the analog-to-digital conversion or start an acquisition delay followed by the analog-to-digital conversion. figure 16-3 shows the operation of the a/d converter after the go bit has been set and the acqt<2:0> bits are cleared. a conversion is started after the following instruction to allow entry into sleep mode before the conversion begins. figure 16-4 shows the operation of the a/d converter after the go bit has been set and the acqt<2:0> bits are set to 010 which selects a 4 t ad acquisition time before the conversion starts. figure 16-3: a/d conversion t ad cycles (a cqt <2:0> = 000 , t acq = 0 ) figure 16-4: a/d conversion t ad cycles (a cqt <2:0> = 010 , t acq = 4 t ad ) note: the go/done bit should not be set in the same instruction that turns on the adc. refer to section 16.2.9 ?a/d conver- sion procedure? . t ad 1 t ad 2 t ad 3 t ad 4 t ad 5 t ad 6 t ad 7 t ad 8 t ad 11 set go bit holding capacitor is disconnected from analog input (typically 100 ns) t ad 9 t ad 10 t cy - t ad adresh:adresl is loaded, go bit is cleared, adif bit is set, holding capacitor is connected to analog input. conversion starts b0 b9 b6 b5 b4 b3 b2 b1 b8 b7 on the following cycle: 2 t ad discharge 1 2 3 4 5 6 7 8 11 set go bit (holding capacitor is disconnected from analog input) 9 10 conversion starts 1 2 3 4 (holding capacitor continues acquiring input) t acqt cycles t ad cycles automatic acquisition time b0 b9 b6 b5 b4 b3 b2 b1 b8 b7 adresh:adresl is loaded, go bit is cleared, adif bit is set, holding capacitor is connected to analog input. on the following cycle: 2 t ad discharge downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 201 pic18(l)f1xk22 16.2.2 completion of a conversion when the conversion is complete, the adc module will: clear the go/done bit set the adif flag bit update the adresh:adresl registers with new conversion result 16.2.3 discharge the discharge phase is used to initialize the value of the capacitor array. the array is discharged after every sample. this feature helps to optimize the unity-gain amplifier, as the circuit always needs to charge the capacitor array, rather than charge/discharge based on previous measure values. 16.2.4 terminating a conversion if a conversion must be terminated before completion, the go/done bit can be cleared by software. the adresh:adresl registers will be updated with the partially complete analog-to-digital conversion sample. unconverted bits will match the last bit converted. 16.2.5 delay between conversions after the a/d conversion is completed or aborted, a 2t ad wait is required before the next acquisition can be started. after this wait, the currently selected channel is reconnected to the charge holding capacitor commencing the next acquisition. 16.2.6 adc operation in power- managed modes the selection of the automatic acquisition time and a/d conversion clock is determined in part by the clock source and frequency while in a power-managed mode. if the a/d is expected to operate while the device is in a power-managed mode, the acqt<2:0> and adcs<2:0> bits in adcon2 should be updated in accordance with the clock source to be used in that mode. after entering the mode, an a/d acquisition or conversion may be started. once started, the device should continue to be clocked by the same clock source until the conversion has been completed. if desired, the device may be placed into the corresponding idle mode during the conversion. if the device clock frequency is less than 1 mhz, the a/d f rc clock source should be selected. 16.2.7 adc operation during sleep the adc module can operate during sleep. this requires the adc clock source to be set to the f rc option. when the f rc clock source is selected, the adc waits one additional instruction before starting the conversion. this allows the sleep instruction to be executed, which can reduce system noise during the conversion. if the adc interrupt is enabled, the device will wake-up from sleep when the conversion completes. if the adc interrupt is disabled, the adc module is turned off after the conversion completes, although the adon bit remains set. when the adc clock source is something other than f rc , a sleep instruction causes the present conversion to be aborted and the adc module is turned off, although the adon bit remains set. 16.2.8 special event trigger the ccp1 special event trigger allows periodic adc measurements without software intervention. when this trigger occurs, the go/done bit is set by hardware and the timer1 or timer3 counter resets to zero. using the special event trigger does not assure proper adc timing. it is the users responsibility to ensure that the adc timing requirements are met. see section 13.3.4 ?special event trigger? for more information. note: a device reset forces all registers to their reset state. thus, the adc module is turned off and any pending conversion is terminated. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 202 ? 2009-2016 microchip technology inc. 16.2.9 a/d conversion procedure this is an example procedure for using the adc to perform an analog-to-digital conversion: 1. configure port: disable pin output driver (see tris register) configure pin as analog 2. configure the adc module: select adc conversion clock configure voltage reference select adc input channel select result format select acquisition delay turn on adc module 3. configure adc interrupt (optional): clear adc interrupt flag enable adc interrupt enable peripheral interrupt enable global interrupt (1) 4. wait the required acquisition time (2) . 5. start conversion by setting the go/done bit. 6. wait for adc conversion to complete by one of the following: polling the go/done bit waiting for the adc interrupt (interrupts enabled) 7. read adc result 8. clear the adc interrupt flag (required if interrupt is enabled). example 16-1: a/d conversion note 1: the global interrupt can be disabled if the user is attempting to wake-up from sleep and resume in-line code execution. 2: software delay required if acqt bits are set to zero delay. see section 16.3 ?a/d acquisition requirements? . ;this code block configures the adc ;for polling, vdd and vss as reference, frc clock and an4 input. ; ;conversion start & polling for completion ; are included. ; movlw b10101111 ;right justify, frc, movwf adcon2 ; & 12 tad acq time movlw b00000000 ;adc ref = vdd,vss movwf adcon1 ; bsf trisc,0 ;set rc0 to input bsf ansel,4 ;set rc0 to analog movlw b00010001 ;an4, adc on movwf adcon0 ; bsf adcon0,go ;start conversion adcpoll: btfsc adcon0,go ;is conversion done? bra adcpoll ;no, test again ; result is complete - store 2 msbits in ; resulthi and 8 lsbits in resultlo movff adresh,resulthi movff adresl,resultlo downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 203 pic18(l)f1xk22 16.2.10 adc register definitions the following registers are used to control the operation of the adc. note: analog pin control is performed by the ansel and anselh registers. for ansel and anselh registers, see register 8-14 and register 8-15 , respectively. register 16-1: adcon0: a/ d control register 0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 chs3 chs2 chs1 chs0 go/done adon bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-6 unimplemented: read as 0 bit 5-2 chs<3:0>: analog channel select bits 0000 = an0 0001 = an1 0010 = an2 0011 = an3 0100 = an4 0101 = an5 0110 = an6 0111 = an7 1000 = an8 1001 = an9 1010 = an10 1011 = an11 1100 = reserved 1101 = reserved 1110 = dac (2) 1111 = fvr (2) bit 1 go/done : a/d conversion status bit 1 = a/d conversion cycle in progress. setting this bit starts an a/d conversion cycle. this bit is automatically cleared by hardware when the a/d conversion has completed. 0 = a/d conversion completed/not in progress bit 0 adon: adc enable bit 1 = adc is enabled 0 = adc is disabled and consumes no operating current note 1: selecting reserved channels will yield unpredictable results as unimplemented input channels are left floating. 2: see section 20.0 ?fixed voltage reference (fvr)? for more information. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 204 ? 2009-2016 microchip technology inc. register 16-2: adcon1: a/ d control register 1 u-0 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 pvcfg1 pvcfg0 nvcfg1 nvcfg0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-4 unimplemented: read as 0 bit 3-2 pvcfg<1:0>: positive voltage reference select bit 00 = positive voltage reference supplied internally by v dd . 01 = positive voltage reference supplied externally through v ref + pin. 10 = positive voltage reference supplied internally through fvr. 11 = reserved. bit 1-0 nvcfg<1:0>: negative voltage reference select bit 00 = negative voltage reference supplied internally by v ss . 01 = negative voltage reference supplied externally through v ref - pin. 10 = reserved. 11 = reserved. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 205 pic18(l)f1xk22 register 16-3: adcon2: a/ d control register 2 r/w-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 adfm acqt2 acqt1 acqt0 adcs2 adcs1 adcs0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 adfm: a/d conversion result format select bit 1 = right justified 0 = left justified bit 6 unimplemented: read as 0 bit 5-3 acqt<2:0>: a/d acquisition time select bits. acquisition time is the duration that the a/d charge holding capacitor remains connected to a/d channel from the instant the go/done bit is set until conversions begins. 000 = 0 (1) 001 = 2 t ad 010 = 4 t ad 011 = 6 t ad 100 = 8 t ad 101 = 12 t ad 110 = 16 t ad 111 = 20 t ad bit 2-0 adcs<2:0>: a/d conversion clock select bits 000 = f osc /2 001 = f osc /8 010 = f osc /32 011 = f rc (1) (clock derived from a dedicated internal oscillator = 600 khz nominal) 100 = f osc /4 101 = f osc /16 110 = f osc /64 111 = f rc (1) (clock derived from a dedicated internal oscillator = 600 khz nominal) note 1: when the a/d clock source is selected as f rc then the start of conversion is delayed by one instruction cycle after the go/done bit is set to allow the sleep instruction to be executed. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 206 ? 2009-2016 microchip technology inc. register 16-4: adresh: adc result register high (adresh) adfm = 0 r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x adres9 adres8 adres7 adres6 adres5 adres4 adres3 adres2 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-0 adres<9:2> : adc result register bits upper 8 bits of 10-bit conversion result register 16-5: adresl: adc result register low (adresl) adfm = 0 r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x adres1 adres0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-6 adres<1:0> : adc result register bits lower 2 bits of 10-bit conversion result bit 5-0 reserved : do not use. register 16-6: adresh: adc result register high (adresh) adfm = 1 r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x adres9 adres8 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-2 reserved : do not use. bit 1-0 adres<9:8> : adc result register bits upper 2 bits of 10-bit conversion result register 16-7: adresl: adc result register low (adresl) adfm = 1 r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x adres7 adres6 adres5 adres4 adres3 adres2 adres1 adres0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-0 adres<7:0> : adc result register bits lower 8 bits of 10-bit conversion result downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 207 pic18(l)f1xk22 16.3 a/d acquisition requirements for the adc to meet its specified accuracy, the charge holding capacitor (c hold ) must be allowed to fully charge to the input channel voltage level. the analog input model is shown in figure 16-5 . the source impedance (r s ) and the internal sampling switch (r ss ) impedance directly affect the time required to charge the capacitor c hold . the sampling switch (r ss ) impedance varies over the device voltage (v dd ), see figure 16-5 . the maximum recommended impedance for analog sources is 10 k ? .. as the source impedance is decreased, the acquisition time may be decreased. after the analog input channel is selected (or changed), an a/d acquisition must be done before the conversion can be started. to calculate the minimum acquisition time, equation 16-1 may be used. this equation assumes that 1/2 lsb error is used (1024 steps for the adc). the 1/2 lsb error is the maximum error allowed for the adc to meet its specified resolution. equation 16-1: acquisition time example t acq amplifier settling time hold capacitor charging time temperature coefficient ++ = t amp t c t coff ++ = 5 s t c temperature - 25c ?? 0.05s/c ?? ?? ++ = t c c hold r ic r ss r s ++ ?? ln(1/2047) ? = 13.5pf 1k ? 700 ? 10k ? ++ ?? ? ln(0.0004885) = 1.20 = s t acq 5 s 1.20s 50c- 25c ?? 0.05s/c ?? ?? ++ = 7.45 s = v applied 1e tc ? rc --------- ? ?? ?? ?? v applied 1 1 2047 ----------- - ? ?? ?? = v applied 1 1 2047 ----------- - ? ?? ?? v chold = v applied 1e t c ? rc --------- - ? ?? ?? ?? v chold = ;[1] v chold charged to within 1/2 lsb ;[2] v chold charge response to v applied ;combining [1] and [2] the value for t c can be approximated with the following equations: solving for t c : therefore: temperature 50c and external impedance of 10k ? 3.0 v v dd = assumptions: note 1: the reference voltage (v ref ) has no effect on the equation, since it cancels itself out. 2: the charge holding capacitor (c hold ) is discharged after each conversion. 3: the maximum recommended impedance for analog sources is 10 k ? . this is required to meet the pin leakage specification. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 208 ? 2009-2016 microchip technology inc. figure 16-5: analog input model figure 16-6: adc transfer function c pin va rs anx 5 pf v dd v t = 0.6v v t = 0.6v i leakage (1) r ic ? 1k sampling switch ss rss c hold = 13.5 pf v ss /v ref - 2.5v rss (k ? ) 2.0v 1.5v .1 1 10 v dd legend: c pin v t i leakage r ic ssc hold = input capacitance = threshold voltage = leakage current at the pin due to = interconnect resistance = sampling switch = sample/hold capacitance various junctions discharge switch 3.0v 3.5v 100 note 1: see section 26.0 ?electrical specifications? . 3ffh 3feh adc output code 3fdh 3fch 004h003h 002h 001h 000h full-scale 3fbh 1/2 lsb ideal v ss /v ref - zero-scale transition v dd /v ref + transition 1/2 lsb ideal full-scale range analog input voltage downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 209 pic18(l)f1xk22 table 16-2: registers associ ated with a/d operation name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page adresh a/d result register, high byte 247 adresl a/d result register, low byte 247 adcon0 chs3 chs2 chs1 chs0 go/done adon 247 adcon1 pvcfg1 pvcfg0 nvcfg1 nvcfg0 247 adcon2 adfm acqt2 acqt1 acqt0 adcs2 adcs1 adcs0 247 ansel ans7 ans6 ans5 ans4 ans3 ans2 ans1 ans0 248 anselh ans11 ans10 ans9 ans8 248 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr1 adip rcip txip sspip ccp1ip tmr2ip tmr1ip 248 pie1 adie rcie txie sspie ccp1ie tmr2ie tmr1ie 248 pir1 adif rcif txif sspif ccp1if tmr2if tmr1if 248 trisa trisa5 trisa4 (1) trisa2 trisa1 trisa0 248 trisb trisb7 trisb6 trisb5 trisb4 248 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 248 legend: = unimplemented, read as 0 . shaded cells are not used for a/d conversion. note 1: unimplemented, read as 1 . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 210 ? 2009-2016 microchip technology inc. 17.0 comparator module comparators are used to interface analog circuits to a digital circuit by comparing two analog voltages and providing a digital indication of their relative magnitudes. the comparators are very useful mixed signal building blocks because they provide analog functionality independent of the program execution. the analog comparator module includes the following features: independent comparator control programmable input selection comparator output is available internally/externally programmable output polarity interrupt-on-change wake-up from sleep programmable speed/power optimization pwm shutdown programmable and fixed voltage reference 17.1 comparator overview a single comparator is shown in figure 17-1 along with the relationship between the analog input levels and the digital output. when the analog voltage at v in + is less than the analog voltage at v in -, the output of the comparator is a digital low level. when the analog voltage at v in + is greater than the analog voltage at v in -, the output of the comparator is a digital high level. figure 17-1: single comparator C + v in + v in - output output v in + v in - note: the black areas of the output of the comparator represents the uncertainty due to input offsets and response time. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 211 pic18(l)f1xk22 figure 17-2: comparator c1 simplified block diagram note 1: when c1on = 0 , the c1 comparator will produce a 0 output to the xor gate. 2: q1 and q3 are phases of the four-phase system clock (f osc ). 3: q1 is held high during sleep mode. 4: positive going pulse generated on both falling and rising edges of the bit. mux c1 c1pol c1out to pwm logic 01 2 3 c1on (1) c1ch<1:0> 2 01 c1r mux rd_cm1con0 set c1if to c1v in - c1v in + c12in0- c12in1- c12in2- c12in3- c1in+ dq en q1 data bus dq en cl q3*rd_cm1con0 nreset + - 01 mux dac output c1rsel fvr c1sp c1v ref c1oe c1out 01 c1sync from tmr1l[0] (4) dq syncc1out downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 212 ? 2009-2016 microchip technology inc. figure 17-3: comparator c2 simplified block diagram mux c2 c2pol c2out to pwm logic 01 2 3 c2on (1) c2ch<1:0> 2 dq en dq en cl rd_cm2con0 q3*rd_cm2con0 q1 set c2if to nreset c2v in - c2v in + c2out pin c12in0- c12in1- c12in2- c12in3- data bus note 1: when c2on = 0 , the c2 comparator will produce a 0 output to the xor gate. 2: q1 and q3 are phases of the four-phase system clock (f osc ). 3: q1 is held high during sleep mode. 4: positive going pulse generated on both falling and rising edges of the bit. 01 c2r mux c2in+ 01 mux c2rsel fvr c2sp c2v ref 01 c2sync c20e dq syncc2out from tmr1l[0] (4) dac output downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 213 pic18(l)f1xk22 17.2 comparator control each comparator has a separate control and configuration register: cm1con0 for comparator c1 and cm2con0 for comparator c2. in addition, comparator c2 has a second control register, cm2con1, for controlling the interaction with timer1 and simultaneous reading of both comparator outputs. the cm1con0 and cm2con0 registers (see registers 17-1 and 17-2 , respectively) contain the control and status bits for the following: enable input selection reference selection output selection output polarity speed selection 17.2.1 comparator enable setting the cxon bit of the cmxcon0 register enables the comparator for operation. clearing the cxon bit disables the comparator resulting in minimum current consumption. 17.2.2 comparator input selection the cxch<1:0> bits of the cmxcon0 register direct one of four analog input pins to the comparator inverting input. 17.2.3 comparator reference selection setting the cxr bit of the cmxcon0 register directs an internal voltage reference or an analog input pin to the noninverting input of the comparator. see section 20.0 ?fixed voltage reference (fvr)? for more information on the internal voltage reference module. 17.2.4 comparator output selection the output of the comparator can be monitored by reading either the cxout bit of the cmxcon0 register or the mcxout bit of the cm2con1 register. in order to make the output available for an external connection, the following conditions must be true: cxoe bit of the cmxcon0 register must be set corresponding tris bit must be cleared cxon bit of the cmxcon0 register must be set 17.2.5 comparator output polarity inverting the output of the comparator is functionally equivalent to swapping the comparator inputs. the polarity of the comparator output can be inverted by setting the cxpol bit of the cmxcon0 register. clearing the cxpol bit results in a non-inverted output. table 17-1 shows the output state versus input conditions, including polarity control. 17.2.6 comparator speed selection the trade-off between speed or power can be optimized during program execution with the cxsp control bit. the default state for this bit is 1 which selects the normal speed mode. device power consumption can be optimized at the cost of slower comparator propagation delay by clearing the cxsp bit to 0 . 17.3 comparator response time the comparator output is indeterminate for a period of time after the change of an input source or the selection of a new reference voltage. this period is referred to as the response time. the response time of the comparator differs from the settling time of the voltage reference. therefore, both of these times must be considered when determining the total response time to a comparator input change. see the comparator and voltage reference specifications in section 26.0 ?electrical specifications? for more details. note: to use cxin+ and c12inx- pins as analog inputs, the appropriate bits must be set in the ansel register and the corresponding tris bits must also be set to disable the output drivers. note 1: the cxoe bit overrides the port data latch. setting the cxon has no impact on the port override. 2: the internal output of the comparator is latched with each instruction cycle. unless otherwise specified, external outputs are not latched. table 17-1: comparator output state vs. input conditions input condition cxpol cxout cxv in - > cxv in + 00 cxv in - < cxv in + 01 cxv in - > cxv in + 11 cxv in - < cxv in + 10 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 214 ? 2009-2016 microchip technology inc. 17.4 comparator interrupt operation the comparator interrupt flag can be set whenever there is a change in the output value of the comparator. changes are recognized by means of a mismatch circuit which consists of two latches and an exclusive- or gate (see figure 17-2 and figure 17-3 ). one latch is updated with the comparator output level when the cmxcon0 register is read. this latch retains the value until the next read of the cmxcon0 register or the occurrence of a reset. the other latch of the mismatch circuit is updated on every q1 system clock. a mismatch condition will occur when a comparator output change is clocked through the second latch on the q1 clock cycle. at this point the two mismatch latches have opposite output levels which is detected by the exclusive-or gate and fed to the interrupt circuitry. the mismatch condition persists until either the cmxcon0 register is read or the comparator output returns to the previous state. the comparator interrupt is set by the mismatch edge and not the mismatch level. this means that the interrupt flag can be reset without the additional step of reading or writing the cmxcon0 register to clear the mismatch registers. when the mismatch registers are cleared, an interrupt will occur upon the comparators return to the previous state, otherwise no interrupt will be generated. software will need to maintain information about the status of the comparator output, as read from the cmxcon0 register, or cm2con1 register, to determine the actual change that has occurred. see figures 17-4 and 17-5 . the cxif bit of the pir2 register is the comparator interrupt flag. this bit must be reset by software by clearing it to 0 . since it is also possible to write a 1 to this register, an interrupt can be generated. in mid-range compatibility mode the cxie bit of the pie2 register and the peie and gie bits of the intcon register must all be set to enable comparator interrupts. if any of these bits are cleared, the interrupt is not enabled, although the cxif bit of the pir2 register will still be set if an interrupt condition occurs. 17.4.1 presetting the mismatch latches the comparator mismatch latches can be preset to the desired state before the comparators are enabled. when the comparator is off the cxpol bit controls the cxout level. set the cxpol bit to the desired cxout non-interrupt level while the cxon bit is cleared. then, configure the desired cxpol level in the same instruction that the cxon bit is set. since all register writes are performed as a read-modify-write, the mismatch latches will be cleared during the instruction read phase and the actual configuration of the cxon and cxpol bits will be occur in the final write phase. figure 17-4: comparator interrupt timing w/o cmxcon0 read figure 17-5: comparator interrupt timing with cmxcon0 read note 1: a write operation to the cmxcon0 register will also clear the mismatch condition because all writes include a read operation at the beginning of the write cycle. 2: comparator interrupts will operate correctly regardless of the state of cxoe. note 1: if a change in the cmxcon0 register (cxout) should occur when a read operation is being executed (start of the q2 cycle), then the cxif interrupt flag of the pir2 register may not get set. 2: when either comparator is first enabled, bias circuitry in the comparator module may cause an invalid output from the comparator until the bias circuitry is stable. allow about 1 ? s for bias settling then clear the mismatch condition and interrupt flags before enabling comparator interrupts. q1q3 cxin+ cxin set cxif (edge) cxif t rt reset by software q1q3 cxin+ cxout set cxif (edge) cxif t rt reset by software cleared by cmxcon0 read downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 215 pic18(l)f1xk22 17.5 operation during sleep the comparator, if enabled before entering sleep mode, remains active during sleep. the additional current consumed by the comparator is shown separately in section 26.0 ?electrical specifications? . if the comparator is not used to wake the device, power consumption can be minimized while in sleep mode by turning off the comparator. each comparator is turned off by clearing the cxon bit of the cmxcon0 register. a change to the comparator output can wake-up the device from sleep. to enable the comparator to wake the device from sleep, the cxie bit of the pie2 register and the peie bit of the intcon register must be set. the instruction following the sleep instruction always executes following a wake from sleep. if the gie bit of the intcon register is also set, the device will then execute the interrupt service routine. 17.6 effects of a reset a device reset forces the cmxcon0 and cm2con1 registers to their reset states. this forces both comparators and the voltage references to their off states. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 216 ? 2009-2016 microchip technology inc. register 17-1: cm1con0: compa rator 1 control register 0 r/w-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 c1on c1out c1oe c1pol c1sp c1r c1ch1 c1ch0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 c1on: comparator c1 enable bit 1 = comparator c1 is enabled 0 = comparator c1 is disabled bit 6 c1out: comparator c1 output bit if c1pol = 1 (inverted polarity): c1out = 0 when c1v in + > c1v in - c1out = 1 when c1v in + < c1v in - if c1pol = 0 (non-inverted polarity): c1out = 1 when c1v in + > c1v in - c1out = 0 when c1v in + < c1v in - bit 5 c1oe: comparator c1 output enable bit 1 = c1out is present on the c1out pin (1) 0 = c1out is internal only bit 4 c1pol: comparator c1 output polarity select bit 1 = c1out logic is inverted 0 = c1out logic is not inverted bit 3 c1sp: comparator c1 speed/power select bit 1 = c1 operates in normal power, higher speed mode 0 = c1 operates in low-power, low-speed mode bit 2 c1r: comparator c1 reference select bit (noninverting input) 1 = c1v in + connects to c1v ref output 0 = c1v in + connects to c12in+ pin bit 1-0 c1ch<1:0>: comparator c1 channel select bit 00 = c12in0- pin of c1 connects to c1v in - 01 = c12in1- pin of c1 connects to c1v in - 10 = c12in2- pin of c1 connects to c1v in - 11 = c12in3- pin of c1 connects to c1v in - note 1: comparator output requires the following three conditions: c1oe = 1 , c1on = 1 and corresponding port tris bit = 0 . downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 217 pic18(l)f1xk22 register 17-2: cm2con0: compa rator 2 control register 0 r/w-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 c2on c2out c2oe c2pol c2sp c2r c2ch1 c2ch0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 c2on: comparator c2 enable bit 1 = comparator c2 is enabled 0 = comparator c2 is disabled bit 6 c2out: comparator c2 output bit if c2pol = 1 (inverted polarity): c2out = 0 when c2v in + > c2v in - c2out = 1 when c2v in + < c2v in - if c2pol = 0 (non-inverted polarity): c2out = 1 when c2v in + > c2v in - c2out = 0 when c2v in + < c2v in - bit 5 c2oe: comparator c2 output enable bit 1 = c2out is present on c2out pin (1) 0 = c2out is internal only bit 4 c2pol: comparator c2 output polarity select bit 1 = c2out logic is inverted 0 = c2out logic is not inverted bit 3 c2sp: comparator c2 speed/power select bit 1 = c2 operates in normal power, higher speed mode 0 = c2 operates in low-power, low-speed mode bit 2 c2r: comparator c2 reference select bits (noninverting input) 1 = c2v in + connects to c2v ref 0 = c2v in + connects to c2in+ pin bit 1-0 c2ch<1:0>: comparator c2 channel select bits 00 = c12in0- pin of c2 connects to c2v in - 01 = c12in1- pin of c2 connects to c2v in - 10 = c12in2- pin of c2 connects to c2v in - 11 = c12in3- pin of c2 connects to c2v in - note 1: comparator output requires the following three conditions: c2oe = 1 , c2on = 1 and corresponding port tris bit = 0 . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 218 ? 2009-2016 microchip technology inc. 17.7 analog input connection considerations a simplified circuit for an analog input is shown in figure 17-6 . since the analog input pins share their connection with a digital input, they have reverse biased esd protection diodes to v dd and v ss . the analog input, therefore, must be between v ss and v dd . if the input voltage deviates from this range by more than 0.6v in either direction, one of the diodes is forward biased and a latch-up may occur. a maximum source impedance of 10 k ? is recommended for the analog sources. also, any external component connected to an analog input pin, such as a capacitor or a zener diode, should have very little leakage current to minimize inaccuracies introduced. figure 17-6: analog input model note 1: when reading a port register, all pins configured as analog inputs will read as a 0 . pins configured as digital inputs will convert as an analog input, according to the input specification. 2: analog levels on any pin defined as a digital input, may cause the input buffer to consume more current than is specified. v a rs < 10k c pin 5 pf v dd v t ? 0.6v v t ? 0.6v r ic i leakage (1) vss a in legend: c pin = input capacitance i leakage = leakage current at the pin due to various junctions r ic = interconnect resistance r s = source impedance v a = analog voltage v t = threshold voltage note 1: see section 26.0 ?electrical specifications? . downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 219 pic18(l)f1xk22 17.8 additional comparator features there are four additional comparator features: simultaneous read of comparator outputs internal reference selection hysteresis selection output synchronization 17.8.1 simultaneous comparator output read the mc1out and mc2out bits of the cm2con1 register are mirror copies of both comparator outputs. the ability to read both outputs simultaneously from a single register eliminates the timing skew of reading separate registers. 17.8.2 internal reference selection there are two internal voltage references available to the noninverting input of each comparator. one of these is the fixed voltage reference (fvr) and the other is the variable digital-to-analog converter (cv ref /dac). the cxrsel bit of the cm2con register determines which of these references is routed to the digital-to-analog converter output (cv ref /dac). further routing to the comparator is accomplished by the cxr bit of the cmxcon0 register. see 20.0 ?fixed voltage reference (fvr)? and figure 17-2 and figure 17-3 for more detail. 17.8.3 comparator hysteresis the comparator cx have selectable hysteresis. the hysteresis can be enabled by setting the cxhys bit of the cm2con1 register. see section 26.0 ?electrical specifications? for more details. 17.8.4 synchronizing comparator output to timer 1 the comparator cx output can be synchronized with timer1 by setting the cxsync bit of the cm2con1 register. when enabled, the cx output is latched on the rising edge of the timer1 source clock. if a prescaler is used with timer1, the comparator output is latched after the prescaling function. to prevent a race condition, the comparator output is latched on the rising edge of the timer1 clock source and timer1 increments on the rising edge of its clock source. see the comparator block diagram ( figure 17-2 and figure 17-3 ) and the timer1 block diagram ( figure 17-2 ) for more information. note 1: obtaining the status of c1out or c2out by reading cm2con1 does not affect the comparator interrupt mismatch registers. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 220 ? 2009-2016 microchip technology inc. register 17-3: cmcon0: compara tor 2 control register 1 r-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 mc1out mc2out c1rsel c2rsel c1hys c2hys c1sync c2sync bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 mc1out: mirror copy of c1out bit bit 6 mc2out: mirror copy of c2out bit bit 5 c1rsel: comparator c1 reference select bit 1 = fvr routed to c1v ref input 0 = cv ref /dac1out routed to c1v ref input bit 4 c2rsel: comparator c2 reference select bit 1 = fvr routed to c2v ref input 0 = cv ref /dac1out routed to c2v ref input bit 3 c1hys: comparator c1 hysteresis enable bit 1 = comparator c1 hysteresis enabled 0 = comparator c1 hysteresis disabled bit 2 c2hys: comparator c2 hysteresis enable bit 1 = comparator c2 hysteresis enabled 0 = comparator c2 hysteresis disabled bit 1 c1sync: c1 output synchronous mode bit 1 = c1 output is synchronous to rising edge to tmr1 clock 0 = c1 output is asynchronous bit 0 c2sync: c2 output synchronous mode bit 1 = c2 output is synchronous to rising edge to tmr1 clock 0 = c2 output is asynchronous downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 221 pic18(l)f1xk22 table 17-2: registers associat ed with comparator module name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page ansel ans7 ans6 ans5 ans4 ans3 ans2 ans1 ans0 248 cm1con0 c1on c1out c1oe c1pol c1sp c1r c1ch1 c1ch0 248 cm2con0 c2on c2out c2oe c2pol c2sp c2r c2ch1 c2ch0 248 cm2con1 mc1out mc2out c1rsel c2rsel c1hys c2hys c1sync c2sync 248 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 245 ipr2 oscfip c1ip c2ip eeip bclip tmr3ip 248 latc latc7 latc6 latc5 latc4 latc3 latc2 latc1 latc0 248 pie2 oscfie c1ie c2ie eeie bclie tmr3ie 248 pir2 oscfif c1if c2if eeif bclif tmr3if 248 portc rc7 rc6 rc5 rc4 rc3 rc2 rc1 rc0 248 vrefcon0 fvr1en fvr1st fvr1s<1:0> 247 vrefcon1 d1en d1lps dac1oe --- d1pss<1:0> d1nss 247 trisa trisa5 trisa4 (1) trisa2 trisa1 trisa0 248 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 248 legend: = unimplemented, read as 0 . shaded cells are unused by the comparator module. note 1: unimplemented, read as 1 . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 222 ? 2009-2016 microchip technology inc. 18.0 power-managed modes pic18(l)f1xk22 devices offer a total of seven operating modes for more efficient power management. these modes provide a variety of options for selective power conservation in applications where resources may be limited (i.e., battery-powered devices). there are three categories of power-managed modes: run modes idle modes sleep mode these categories define which portions of the device are clocked and sometimes, what speed. the run and idle modes may use any of the three available clock sources (primary, secondary or internal oscillator block); the sleep mode does not use a clock source. the power-managed modes include several power- saving features offered on previous pic ? microcontroller devices. one is the clock switching feature which allows the controller to use the timer1 oscillator in place of the primary oscillator. also included is the sleep mode, offered by all pic microcontroller devices, where all device clocks are stopped. 18.1 selecting power-managed modes selecting a power-managed mode requires two decisions: whether or not the cpu is to be clocked the selection of a clock source the idlen bit of the osccon register controls cpu clocking, while the scs<1:0> bits of the osccon register select the clock source. the individual modes, bit settings, clock sources and affected modules are summarized in table 18-1 . 18.1.1 clock sources the scs<1:0> bits allow the selection of one of three clock sources for power-managed modes. they are: the primary clock, as defined by the fosc<3:0> configuration bits the secondary clock (the timer1 oscillator) the internal oscillator block 18.1.2 entering power-managed modes switching from one power-managed mode to another begins by loading the osccon register. the scs<1:0> bits select the clock source and determine which run or idle mode is to be used. changing these bits causes an immediate switch to the new clock source, assuming that it is running. the switch may also be subject to clock transition delays. refer to section 2.9 ?clock switching? for more information. entry to the power-managed idle or sleep modes is triggered by the execution of a sleep instruction. the actual mode that results depends on the status of the idlen bit of the osccon register. depending on the current mode and the mode being switched to, a change to a power-managed mode does not always require setting all of these bits. many transitions may be done by changing the oscillator select bits, or changing the idlen bit, prior to issuing a sleep instruction. if the idlen bit is already configured correctly, it may only be necessary to perform a sleep instruction to switch to the desired mode. table 18-1: power-managed modes mode osccon bits module clocking available clock and oscillator source idlen (1) scs<1:0> cpu peripherals sleep 0 n/a off off none C all clocks are disabled pri_run n/a 00 clocked clocked primary C lp, xt, hs, rc, ec and internal oscillator block (2) . this is the normal full power execution mode. sec_run n/a 01 clocked clocked secondary C timer1 oscillator rc_run n/a 1x clocked clocked internal oscillator block (2) pri_idle 10 0 off clocked primary C lp, xt, hs, hspll, rc, ec sec_idle 10 1 off clocked secondary C timer1 oscillator rc_idle 11 x off clocked internal oscillator block (2) note 1: idlen reflects its value when the sleep instruction is executed. 2: includes hfintosc and hfintosc postscal er, as well as the lfintosc source. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 223 pic18(l)f1xk22 18.1.3 multiple functions of the sleep command the power-managed mode that is invoked with the sleep instruction is determined by the setting of the idlen bit of the osccon register at the time the instruction is executed. all clocks stop and minimum power is consumed when sleep is executed with the idlen bit cleared. the system clock continues to supply a clock to the peripherals but is disconnected from the cpu when sleep is executed with the idlen bit set. 18.2 run modes in the run modes, clocks to both the core and peripherals are active. the difference between these modes is the clock source. 18.2.1 pri_run mode the pri_run mode is the normal, full power execution mode of the microcontroller. this is also the default mode upon a device reset, unless two-speed start-up is enabled (see section 2.11 ?two-speed start-up mode? for details). in this mode, the device operated off the oscillator defined by the fosc bits of the configh configuration register. 18.2.2 sec_run mode in sec_run mode, the cpu and peripherals are clocked from the secondary external oscillator. this gives users the option of lower power consumption while still using a high accuracy clock source. sec_run mode is entered by setting the scs<1:0> bits of the osccon register to 01 . when sec_run mode is active all of the following are true: the main clock source is switched to the secondary external oscillator primary external oscillator is shut down t1run bit of the t1con register is set osts bit is cleared. 18.2.3 rc_run mode in rc_run mode, the cpu and peripherals are clocked from the internal oscillator. in this mode, the primary external oscillator is shut down. rc_run mode provides the best power conservation of all the run modes when the lfintosc is the system clock. rc_run mode is entered by setting the scs1 bit. when the clock source is switched from the primary oscillator to the internal oscillator, the primary oscillator is shut down and the osts bit is cleared. the ircf bits may be modified at any time to immediately change the clock speed. note: the secondary external oscillator should already be running prior to entering sec_run mode. if the t1oscen bit is not set when the scs<1:0> bits are set to 01 , entry to sec_run mode will not occur until t1oscen bit is set and secondary external oscillator is ready. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 224 ? 2009-2016 microchip technology inc. 18.3 sleep mode the power-managed sleep mode in the pic18(l)f1xk22 devices is identical to the legacy sleep mode offered in all other pic microcontroller devices. it is entered by clearing the idlen bit of the osccon register and executing the sleep instruction. this shuts down the selected oscillator ( figure 18-1 ) and all clock source status bits are cleared. entering the sleep mode from either run or idle mode does not require a clock switch. this is because no clocks are needed once the controller has entered sleep. if the wdt is selected, the lfintosc source will continue to operate. if the timer1 oscillator is enabled, it will also continue to run. when a wake event occurs in sleep mode (by interrupt, reset or wdt time-out), the device will not be clocked until the clock source selected by the scs<1:0> bits becomes ready (see figure 18-2 ), or it will be clocked from the internal oscillator block if either the two-speed start-up or the fail-safe clock monitor are enabled (see section 23.0 ?special features of the cpu? ). in either case, the osts bit is set when the primary clock is providing the device clocks. the idlen and scs bits are not affected by the wake-up. 18.4 idle modes the idle modes allow the controllers cpu to be selectively shut down while the peripherals continue to operate. selecting a particular idle mode allows users to further manage power consumption. if the idlen bit is set to a 1 when a sleep instruction is executed, the peripherals will be clocked from the clock source selected by the scs<1:0> bits; however, the cpu will not be clocked. the clock source status bits are not affected. setting idlen and executing a sleep instruction provides a quick method of switching from a given run mode to its corresponding idle mode. if the wdt is selected, the lfintosc source will continue to operate. if the timer1 oscillator is enabled, it will also continue to run. since the cpu is not executing instructions, the only exits from any of the idle modes are by interrupt, wdt time-out, or a reset. when a wake event occurs, cpu execution is delayed by an interval of t csd while it becomes ready to execute code. when the cpu begins executing code, it resumes with the same clock source for the current idle mode. for example, when waking from rc_idle mode, the internal oscillator block will clock the cpu and peripherals (in other words, rc_run mode). the idlen and scs bits are not affected by the wake-up. while in any idle mode or the sleep mode, a wdt time-out will result in a wdt wake-up to the run mode currently specified by the scs<1:0> bits. figure 18-1: transition timi ng for entry to sleep mode figure 18-2: transition timing for wake from sleep (hspll) q4 q3 q2 osc1 peripheral sleep program q1 q1 counter clock cpu clock pc + 2 pc q3 q4 q1 q2 osc1 peripheral program pc pll clock q3 q4 output cpu clock q1 q2 q3 q4 q1 q2 clock counter pc + 6 pc + 4 q1 q2 q3 q4 wake event note1: t ost = 1024 t osc ; t pll = 2 ms (approx). these intervals are not shown to scale. t ost (1) t pll (1) osts bit set pc + 2 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 225 pic18(l)f1xk22 18.4.1 pri_idle mode this mode is unique among the three low-power idle modes, in that it does not disable the primary device clock. for timing sensitive applications, this allows for the fastest resumption of device operation with its more accurate primary clock source, since the clock source does not have to warm-up or transition from another oscillator. pri_idle mode is entered from pri_run mode by setting the idlen bit and executing a sleep instruction. if the device is in another run mode, set idlen first, then clear the scs bits and execute sleep . although the cpu is disabled, the peripherals continue to be clocked from the primary clock source specified by the fosc<3:0> configuration bits. the osts bit remains set (see figure 18-3 ). when a wake event occurs, the cpu is clocked from the primary clock source. a delay of interval t csd is required between the wake event and when code execution starts. this is required to allow the cpu to become ready to execute instructions. after the wake- up, the osts bit remains set. the idlen and scs bits are not affected by the wake-up (see figure 18-4 ). 18.4.2 sec_idle mode in sec_idle mode, the cpu is disabled but the peripherals continue to be clocked from the timer1 oscillator. this mode is entered from sec_run by setting the idlen bit and executing a sleep instruction. if the device is in another run mode, set the idlen bit first, then set the scs<1:0> bits to 01 and execute sleep . when the clock source is switched to the timer1 oscillator, the primary oscillator is shut down, the osts bit is cleared and the t1run bit is set. when a wake event occurs, the peripherals continue to be clocked from the timer1 oscillator. after an interval of t csd following the wake event, the cpu begins executing code being clocked by the timer1 oscillator. the idlen and scs bits are not affected by the wake-up; the timer1 oscillator continues to run (see figure 18-4 ). figure 18-3: transition timing for entry to idle mode figure 18-4: transition timing for wake from idle to run mode note: the timer1 oscillator should already be running prior to entering sec_idle mode. if the t1oscen bit is not set when the sleep instruction is executed, the main system clock will continue to operate in the previously selected mode and the corresponding idle mode will be entered (i.e., pri_idle or rc_idle). q1 peripheral program pc pc + 2 osc1 q3 q4 q1 cpu clock clock counter q2 osc1 peripheral program pc cpu clock q1 q3 q4 clock counter q2 wake event t csd downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 226 ? 2009-2016 microchip technology inc. 18.4.3 rc_idle mode in rc_idle mode, the cpu is disabled but the peripherals continue to be clocked from the internal oscillator block from the hfintosc multiplexer output. this mode allows for controllable power conservation during idle periods. from rc_run, this mode is entered by setting the idlen bit and executing a sleep instruction. if the device is in another run mode, first set idlen, then set the scs1 bit and execute sleep . it is recommended that scs0 also be cleared, although its value is ignored, to maintain software compatibility with future devices. the hfintosc multiplexer may be used to select a higher clock frequency by modifying the ircf bits before executing the sleep instruction. when the clock source is switched to the hfintosc multiplexer, the primary oscillator is shut down and the osts bit is cleared. if the ircf bits are set to any non-zero value, or the intsrc bit is set, the hfintosc output is enabled. the iosf bit becomes set, after the hfintosc output becomes stable, after an interval of t iobst . clocks to the peripherals continue while the hfintosc source stabilizes. if the ircf bits were previously at a non- zero value, or intsrc was set before the sleep instruction was executed and the hfintosc source was already stable, the iosf bit will remain set. if the ircf bits and intsrc are all clear, the hfintosc output will not be enabled, the iosf bit will remain clear and there will be no indication of the current clock source. when a wake event occurs, the peripherals continue to be clocked from the hfintosc multiplexer output. after a delay of t csd following the wake event, the cpu begins executing code being clocked by the hfintosc multiplexer. the idlen and scs bits are not affected by the wake-up. the lfintosc source will continue to run if either the wdt or the fail-safe clock monitor is enabled. 18.5 exiting idle and sleep modes an exit from sleep mode or any of the idle modes is triggered by any one of the following: an interrupt a reset a watchdog time-out this section discusses the triggers that cause exits from power-managed modes. the clocking subsystem actions are discussed in each of the power-managed modes (see section 18.2 ?run modes? , section 18.3 ?sleep mode? and section 18.4 ?idle modes? ). 18.5.1 exit by interrupt any of the available interrupt sources can cause the device to exit from an idle mode or the sleep mode to a run mode. to enable this functionality, an interrupt source must be enabled by setting its enable bit in one of the intcon or pie registers. the peie bit must also be set if the desired interrupt enable bit is in a pie register. the exit sequence is initiated when the corresponding interrupt flag bit is set. the instruction immediately following the sleep instruction is executed on all exits by interrupt from idle or sleep modes. code execution then branches to the interrupt vector if the gie/gieh bit of the intcon register is set, otherwise code execution continues without branching (see section 7.0 ?interrupts? ). a fixed delay of interval t csd following the wake event is required when leaving sleep and idle modes. this delay is required for the cpu to prepare for execution. instruction execution resumes on the first clock cycle following this delay. 18.5.2 exit by wdt time-out a wdt time-out will cause different actions depending on which power-managed mode the device is in when the time-out occurs. if the device is not executing code (all idle modes and sleep mode), the time-out will result in an exit from the power-managed mode (see section 18.2 ?run modes? and section 18.3 ?sleep mode? ). if the device is executing code (all run modes), the time-out will result in a wdt reset (see section 23.2 ?watchdog timer (wdt)? ). the wdt timer and postscaler are cleared by any one of the following: executing a sleep instruction executing a clrwdt instruction the loss of the currently selected clock source when the fail-safe clock monitor is enabled modifying the ircf bits in the osccon register when the internal oscillator block is the device clock source downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 227 pic18(l)f1xk22 18.5.3 exit by reset exiting sleep and idle modes by reset causes code execution to restart at address 0. see section 22.0 ?reset? for more details. the exit delay time from reset to the start of code execution depends on both the clock sources before and after the wake-up and the type of oscillator. exit delays are summarized in table 18-2 . 18.5.4 exit without an oscillator start-up delay certain exits from power-managed modes do not invoke the ost at all. there are two cases: pri_idle mode, where the primary clock source is not stopped and the primary clock source is not any of the lp, xt, hs or hspll modes. in these instances, the primary clock source either does not require an oscillator start-up delay since it is already running (pri_idle), or normally does not require an oscillator start-up delay (rc, ec, intosc, and intoscio modes). however, a fixed delay of interval t csd following the wake event is still required when leaving sleep and idle modes to allow the cpu to prepare for execution. instruction execution resumes on the first clock cycle following this delay. table 18-2: exit delay on wake-up by reset from sleep mode or any idle mode (by clock sources) clock source before wake-up clock source after wake-up exit delay clock ready status bit (osccon) primary device clock (pri_idle mode) lp, xt, hs t csd (1) osts hspll ec, rc hfintosc (2) iosf t1osc or lfintosc (1) lp, xt, hs t ost (3) osts hspll t ost + t pll (3) ec, rc t csd (1) hfintosc (1) t iobst (4) iosf hfintosc (2) lp, xt, hs t ost (4) osts hspll t ost + t pll (3) ec, rc t csd (1) hfintosc (1) none iosf none (sleep mode) lp, xt, hs t ost (3) osts hspll t ost + t pll (3) ec, rc t csd (1) hfintosc (1) t iobst (4) iosf note 1: t csd is a required delay when waking from sleep and all idle modes and runs concurrently with any other required delays (see section 18.4 ?idle modes? ). on reset, hfintosc defaults to 1 mhz. 2: includes both the hfintosc 16 mhz source and postscaler derived frequencies. 3: t ost is the oscillator start-up timer. t pll is the pll lock-out timer (parameter f12 ). 4: execution continues during the hfintosc stabilization period, t iobst . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 228 ? 2009-2016 microchip technology inc. 19.0 sr latch the module consists of a single sr latch with multiple set and reset inputs as well as selectable latch output. the sr latch module includes the following features: programmable input selection sr latch output is available internally/externally selectable q and q output firmware set and reset sr latch 19.1 latch operation the latch is a set-reset latch that does not depend on a clock source. each of the set and reset inputs are active-high. the latch can be set or reset by cxout, int1 pin, or variable clock. additionally the srps and the srpr bits of the srcon0 register may be used to set or reset the sr latch, respectively. the latch is reset-dominant, therefore, if both set and reset inputs are high the latch will go to the reset state. both the srps and srpr bits are self resetting which means that a single write to either of the bits is all that is necessary to complete a latch set or reset operation. 19.2 latch output the srqen and srnqen bits of the srcon0 register control the latch output selection. both of the sr latchs outputs may be directly output to an independent i/o pin. control is determined by the state of bits srqen and srnqen in registers srcon0. the applicable tris bit of the corresponding port must be cleared to enable the port pin output driver. 19.3 effects of a reset upon any device reset, the sr latch is not initialized. the users firmware is responsible to initialize the latch output before enabling it to the output pins. figure 19-1: sr latch simplified block diagram srps s r q q note 1: if r = 1 and s = 1 simultaneously, q = 0 , q = 1 2: pulse generator causes a 2 q-state pulse width. 3: output shown for reference only. see i /o port pin block diagram for more detail. 4: name denotes the source of connection at the comparator output. pulse gen ( 2 ) sr latch (1) srq pin (3) srqen srlen srspe srsc2e int1 srscke srclk syncc2out (4) srsc1e syncc1out (4) srpr pulse gen ( 2 ) srrpe srrc2e int1 srrcke srclk syncc2out (4) srrc1e syncc1out (4) srnqen srlen srnq pin (3) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 229 pic18(l)f1xk22 table 19-1: srclk frequency table srclk divider f osc = 20 mhz f osc = 16 mhz f osc = 8 mhz f osc = 4 mhz f osc = 1 mhz 111 512 25.6 ? s 32 ? s6 4 ? s 128 ? s 512 ? s 110 256 12.8 ? s 16 ? s3 2 ? s6 4 ? s 256 ? s 101 128 6.4 ? s8 ? s1 6 ? s3 2 ? s 128 ? s 100 64 3.2 ? s4 ? s8 ? s1 6 ? s6 4 ? s 011 32 1.6 ? s2 ? s4 ? s8 ? s3 2 ? s 010 16 0.8 ? s1 ? s2 ? s4 ? s1 6 ? s 001 80 . 4 ? s0 . 5 ? s1 ? s2 ? s8 ? s 000 40 . 2 ? s0 . 2 5 ? s0 . 5 ? s1 ? s4 ? s register 19-1: srcon0: sr latch control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 srlen srclk2 srclk1 srclk0 srqen srnqen srps srpr bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented c = clearable only bit -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 srlen: sr latch enable bit (1) 1 = sr latch is enabled 0 = sr latch is disabled bit 6-4 srclk<2:0> (1) : sr latch clock divider bits 000 = 1/4 peripheral cycle clock 001 = 1/8 peripheral cycle clock 010 = 1/16 peripheral cycle clock 011 = 1/32 peripheral cycle clock 100 = 1/64 peripheral cycle clock 101 = 1/128 peripheral cycle clock 110 = 1/256 peripheral cycle clock 111 = 1/512 peripheral cycle clock bit 3 srqen: sr latch q output enable bit 1 = q is present on the srq pin 0 = q is internal only bit 2 srnqen: sr latch q output enable bit 1 =q is present on the srnq pin 0 =q is internal only bit 1 srps: pulse set input of the sr latch bit 1 = pulse input 0 = always reads back 0 bit 0 srpr: pulse reset input of the sr latch bit 1 = pulse input 0 = always reads back 0 note 1: changing the srclk bits while the sr latch is enabled may cause false triggers to the set and reset inputs of the latch. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 230 ? 2009-2016 microchip technology inc. table 19-2: registers associated with the sr latch register 19-2: srcon1: sr latch control register 1 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 srspe srscke srsc2e srsc1e srrpe srrcke srrc2e srrc1e bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented c = clearable only bit -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 srspe: sr latch peripheral set enable bit 1 = int1 pin status sets sr latch 0 = int1 pin status has no effect on sr latch bit 6 srscke: sr latch set clock enable bit 1 = set input of sr latch is pulsed with srclk 0 = set input of sr latch is not pulsed with srclk bit 5 srsc2e: sr latch c2 set enable bit 1 = c2 comparator output sets sr latch 0 = c2 comparator output has no effect on sr latch bit 4 srsc1e: sr latch c1 set enable bit 1 = c1 comparator output sets sr latch 0 = c1 comparator output has no effect on sr latch bit 3 srrpe: sr latch peripheral reset enable bit 1 = int1 pin resets sr latch 0 = int1 pin has no effect on sr latch bit 2 srrcke: sr latch reset clock enable bit 1 = reset input of sr latch is pulsed with srclk 0 = reset input of sr latch is not pulsed with srclk bit 1 srrc2e: sr latch c2 reset enable bit 1 = c2 comparator output resets sr latch 0 = c2 comparator output has no effect on sr latch bit 0 srrc1e: sr latch c1 reset enable bit 1 = c1 comparator output resets sr latch 0 = c1 comparator output has no effect on sr latch name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page cm2con1 mc1out mc2out c1rsel c2rsel c1hys c2hys c1sync c2sync 248 intcon3 int2ip int1ip int2ie int1ie int2if int1if 245 srcon0 srlen srclk2 srclk1 srclk0 srqen srnqen srps srpr 248 srcon1 srspe srscke srsc2e srsc1e srrpe srrcke srrc2e srrc1e 248 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 248 legend: shaded cells are not used with the sr latch module. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 231 pic18(l)f1xk22 20.0 fixed voltage reference (fvr) the fixed voltage reference, or fvr, is a stable voltage reference, independent of v dd , with 1.024v, 2.048v or 4.096v selectable output levels. the output of the fvr can be configured to supply a reference voltage to the following: adc input channel adc positive reference comparator positive input digital-to-analog converter (dac) the fvr can be enabled by setting the fvr1en bit of the vrefcon0 register. 20.1 independent gain amplifiers the output of the fvr supplied to the adc, comparators and dac is routed through an independent programmable gain amplifier. the amplifier can be configured to amplify the 1.024v reference voltage by 1x, 2x or 4x, to produce the three possible voltage levels. the fvr1s<1:0> bits of the vrefcon0 register are used to enable and configure the gain amplifier settings for the reference supplied to the dac and comparator modules. when the adc module is configured to use the fvr output, (fvr1buf2) the reference is buffered through an additional unity gain amplifier. this buffer is disabled if the adc is not configured to use the fvr. for specific use of the fvr, refer to the specific module sections: section 16.0 ?analog-to-digital converter (adc) module? , section 21.0 ?digital-to-analog converter (dac) module? and section 17.0 ?com- parator module? . 20.2 fvr stabilization period when the fixed voltage reference module is enabled, it requires time for the reference and amplifier circuits to stabilize. once the circuits stabilize and are ready for use, the fvr1st bit of the vrefcon0 register will be set. see table 26-23 for the minimum delay requirement. figure 20-1: voltage reference block diagram to comparators, dac x1 x2 x4 + - fvr1en fvr1st 2 fvr1s<1:0> to adc module x1 x2 x4 fvr1buf1 fvr1buf2 1.024v fixed voltage reference note 1: fvr_buf2_enable = 1 when (adon = 1) and [(pvcfg<1:0> = 10 ) or ( chs<4:0> = 11111 )] fvr_buf2_enable (1) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 232 ? 2009-2016 microchip technology inc. 20.3 register definitions: fvr control register 20-1: vrefcon0: fixed vo ltage reference control register r/w-0 r/w-0 r/w-0 r/w-1 u-0 u-0 u-0 u-0 fvr1en fvr1st fvr1s<1:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets 1 = bit is set 0 = bit is cleared bit 7 fvr1en: fixed voltage reference enable bit 0 = fixed voltage reference is disabled 1 = fixed voltage reference is enabled bit 6 fvr1st: fixed voltage reference ready flag bit 0 = fixed voltage reference output is not ready or not enabled 1 = fixed voltage reference output is ready for use bit 5-4 fvr1s<1:0>: fixed voltage reference selection bits 00 = fixed voltage reference peripheral output is off 01 = fixed voltage reference peripheral output is 1x (1.024v) 10 = fixed voltage reference peripheral output is 2x (2.048v) (1) 11 = fixed voltage reference peripheral output is 4x (4.096v) (1) bit 3-2 reserved: read as 0 . maintain these bits clear. bit 1-0 unimplemented: read as 0 . note 1: fixed voltage reference output cannot exceed v dd . table 20-1: summary of registers asso ciated with fixed voltage reference name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page vrefcon0 fvr1en fvr1st fvr1s<1:0> 232 legend: = unimplemented locations, read as 0 . shaded bits are not used by the fvr module. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 233 pic18(l)f1xk22 21.0 digital-to-analog converter (dac) module the digital-to-analog converter supplies a variable voltage reference, ratiometric with the input source, with 32 selectable output levels. the input of the dac can be connected to: external v ref pins v dd supply voltage fvr (fixed voltage reference) the output of the dac can be configured to supply a reference voltage to the following: comparator positive input adc input channel dac1out pin the digital-to-analog converter (dac) can be enabled by setting the d1en bit of the vrefcon1 register. 21.1 output voltage selection the dac has 32 voltage level ranges. the 32 levels are set with the dac1r<4:0> bits of the vrefcon2 register. the dac output voltage is determined by the following equations: equation 21-1: dac output voltage 21.2 ratiometric output level the dac output value is derived using a resistor ladder with each end of the ladder tied to a positive and negative voltage reference input source. if the voltage of either input source fluctuates, a similar fluctuation will result in the dac output value. the value of the individual resistors within the ladder can be found in section 26.0 ?electrical specifications? . 21.3 low-power voltage state in order for the dac module to consume the least amount of power, one of the two voltage reference input sources to the resistor ladder must be disconnected. either the positive voltage source, (v src +), or the negative voltage source, (v src -) can be disabled. the negative voltage source is disabled by setting the d1lps bit in the vrefcon1 register. clearing the d1lps bit in the vrefcon1 register disables the positive voltage source. 21.4 output clamped to positive voltage source the dac output voltage can be set to v src + with the least amount of power consumption by performing the following: clearing the d1en bit in the vrefcon1 register. setting the d1lps bit in the vrefcon1 register. configuring the d1pss bits to the proper positive source. configuring the dac1rx bits to 11111 in the vrefcon2 register. this is also the method used to output the voltage level from the fvr to an output pin. see section 21.6 ?dac voltage reference output? for more information. 21.5 output clamped to negative voltage source the dac output voltage can be set to v src - with the least amount of power consumption by performing the following: clearing the d1en bit in the vrefcon1 register. clearing the dac1r bit in the vrefcon1 register. configuring the d1pss bits to the proper negative source. configuring the dac1rx bits to 00000 in the vrefcon2 register. this allows the comparator to detect a zero-crossing while not consuming additional current through the dac module. 21.6 dac voltage reference output the dac can be output to the dac1out (cv ref ) pin by setting the dac1oe bit of the vrefcon1 register to 1 . selecting the dac reference voltage for output on the dac1out pin automatically overrides the digital output buffer and digital input threshold detector functions of that pin. reading the dac1out pin when it has been configured for dac reference voltage output will always return a 0 . due to the limited current drive capability, a buffer must be used on the dac voltage reference output for external connections to dac1out. figure 21-2 shows an example buffering technique. v out v src +v src - ? ?? dacr<4:0> 2 5 ------------------------------ - ? ?? ?? = + v src - v src + = v dd , v ref + or fvr1 v src - = v ss or v ref - downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 234 ? 2009-2016 microchip technology inc. figure 21-1: digital-to-analog co nverter block diagram figure 21-2: voltage reference ou tput buffer example 32-to-1 mux dac1r<4:0> r v ref - d1nss rr r r r r 32 dac output cv ref /dac1out 5 (to comparators and adc modules) dac1oe v dd v ref + d1pss<1:0> 2 d1en steps digital-to-analog converter (dac) fvr1buf1 r v src - v src + v ss d1lps 11111 11110 00001 00000 1 0 reserved 11 10 01 00 dac1out buffered dac output +C dac module voltage reference output impedance r pic ? mcu downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 235 pic18(l)f1xk22 21.7 operation during sleep when the device wakes up from sleep through an interrupt or a watchdog timer time-out, the contents of the vrefcon1 register are not affected. to minimize current consumption in sleep mode, the voltage reference should be disabled. 21.8 effects of a reset a device reset affects the following: dac is disabled dac output voltage is removed from the dac1out pin the dac1r<4:0> range select bits are cleared 21.9 register definitions: dac control register 21-1: vrefcon1: voltage reference control register 0 r/w-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 u-0 r/w-0 d1en d1lps dac1oe d1pss<1:0> d1nss bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets 1 = bit is set 0 = bit is cleared bit 7 d1en: dac enable bit 1 = dac is enabled 0 = dac is disabled bit 6 d1lps: dac low-power voltage source select bit 1 = dac positive reference source selected 0 = dac negative reference source selected bit 5 dac1oe: dac voltage output enable bit 1 = dac voltage level is also an output on the dac1out (cv ref ) pin 0 = dac voltage level is disconnected from the dac1out (cv ref ) pin bit 4 unimplemented: read as 0 bit 3-2 d1pss<1:0>: dac positive source select bits 00 =v dd 01 =v ref + 10 = fvr1buf1 output 11 = reserved, do not use bit 1 unimplemented: read as 0 bit 0 d1nss: dac negative source select bits 1 =v ref - 0 =v ss downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 236 ? 2009-2016 microchip technology inc. register 21-2: vrefcon2: voltage reference control register 1 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 dac1r<4:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets 1 = bit is set 0 = bit is cleared bit 7-5 unimplemented: read as 0 bit 4-0 dac1r<4:0>: dac voltage output select bits v out = ((v src +) - (v src -))*(dac1r<4:0>/(2 5 )) + v src - table 21-1: registers asso ciated with dac module name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page vrefcon0 fvr1en fvr1st fvr1s<1:0> 232 vrefcon1 d1en d1lps dac1oe d 1 p s s < 1 : 0 > d 1 n s s 235 vrefcon2 d a c 1 r < 4 : 0 > 236 legend: = unimplemented locations, read as 0 . shaded bits are not used by the dac module. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 237 pic18(l)f1xk22 22.0 reset the pic18(l)f1xk22 devices differentiate between various kinds of reset: a) power-on reset (por) b) mclr reset during normal operation c) mclr reset during power-managed modes d) watchdog timer (wdt) reset (during execution) e) programmable brown-out reset (bor) f) reset instruction g) stack full reset h) stack underflow reset this section discusses resets generated by mclr , por and bor and covers the operation of the various start-up timers. stack reset events are covered in section 3.1.2.4 ?stack overflow and underflow resets? . wdt resets are covered in section 23.2 ?watchdog timer (wdt)? . a simplified block diagram of the on-chip reset circuit is shown in figure 22-1 . 22.1 rcon register device reset events are tracked through the rcon register ( register 22-1 ). the lower five bits of the register indicate that a specific reset event has occurred. in most cases, these bits can only be cleared by the event and must be set by the application after the event. the state of these flag bits, taken together, can be read to indicate the type of reset that just occurred. this is described in more detail in section 22.6 ?reset state of registers? . the rcon register also has control bits for setting interrupt priority (ipen) and software control of the bor (sboren). interrupt priority is discussed in section 7.0 ?interrupts? . bor is covered in section 22.4 ?brown-out reset (bor)? . figure 22-1: simplified block diagram of on-chip reset circuit external reset mclr v dd osc1 wdt time-out v dd rise detect ost/pwrt lfintosc por pulse ost (2) 10-bit ripple counter pwrt (2) 11-bit ripple counter enable ost (1) enable pwrt note 1: see table 22-2 for time-out situations. 2: pwrt and ost counters are reset by por and bor. see sections 22.3 and 22.4 . brown-out reset boren reset instruction stack pointer stack full/underflow reset sleep ( )_idle 1024 cycles 65.5 ms 32 ? s mclre s r q chip_reset downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 238 ? 2009-2016 microchip technology inc. register 22-1: rcon: re set control register r/w-0 r/w-1 u-0 r/w-1 r-1 r-1 r/w-0 r/w-0 ipen sboren (1) r i to pd por (2) bor bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7 ipen: interrupt priority enable bit 1 = enable priority levels on interrupts 0 = disable priority levels on interrupts (pic16cxxx compatibility mode) bit 6 sboren: bor software enable bit (1) if boren<1:0> = 01 : 1 = bor is enabled 0 = bor is disabled if boren<1:0> = 00 , 10 or 11 : bit is disabled and read as 0 . bit 5 unimplemented: read as 0 bit 4 ri : reset instruction flag bit 1 = the reset instruction was not executed (set by firmware or power-on reset) 0 = the reset instruction was executed causing a device reset (must be set in firmware after a code-executed reset occurs) bit 3 to : watchdog time-out flag bit 1 = set by power-up, clrwdt instruction or sleep instruction 0 = a wdt time-out occurred bit 2 pd : power-down detection flag bit 1 = set by power-up or by the clrwdt instruction 0 = set by execution of the sleep instruction bit 1 por : power-on reset status bit (2) 1 = no power-on reset occurred 0 = a power-on reset occurred (must be set in software after a power-on reset occurs) bit 0 bor : brown-out reset status bit (3) 1 = a brown-out reset has not occurred (set by firmware only) 0 = a brown-out reset occurred (must be set by firmware after a por or brown-out reset occurs) note 1: if sboren is enabled, its reset state is 1 ; otherwise, it is 0 . 2: the actual reset value of por is determined by the type of device reset. see the notes following this register and section 22.6 ?reset state of registers? for additional information. 3: see table 22-3 . downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 239 pic18(l)f1xk22 22.2 master clear (mclr ) the mclr pin provides a method for triggering an external reset of the device. a reset is generated by holding the pin low. these devices have a noise filter in the mclr reset path which detects and ignores small pulses. the mclr pin is not driven low by any internal resets, including the wdt. in pic18(l)f1xk22 devices, the mclr input can be disabled with the mclre configuration bit. when mclr is disabled, the pin becomes a digital input. see section 8.1 ?porta, trisa and lata registers? for more information. 22.3 power-on reset (por) a power-on reset pulse is generated on-chip whenever v dd rises above a certain threshold. this allows the device to start in the initialized state when v dd is adequate for operation. to take advantage of the por circuitry, tie the mclr pin through a resistor (1 k ? to 10 k ? ) to v dd . this will eliminate external rc components usually needed to create a power-on reset delay. when the device starts normal operation (i.e., exits the reset condition), device operating parameters (voltage, frequency, temperature, etc.) must be met to ensure operation. if these conditions are not met, the device must be held in reset until the operating conditions are met. por events are captured by the por bit of the rcon register. the state of the bit is set to 0 whenever a por occurs; it does not change for any other reset event. por is not reset to 1 by any hardware event. to capture multiple events, the user must manually set the bit to 1 by software following any por. figure 22-2: external power-on reset circuit (for slow v dd power-up) note 1: external power-on reset circuit is required only if the v dd power-up slope is too slow. the diode d helps discharge the capacitor quickly when v dd powers down. 2: r < 40 k ? is recommended to make sure that the voltage drop across r does not violate the devices electric al specification. 3: r1 ? 1 k ? will limit any current flowing into mclr from external capacitor c, in the event of mclr /v pp pin breakdown, due to electrostatic discharge (esd) or electrical overstress (eos). c r1 r d v dd mclr v dd pic ? mcu downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 240 ? 2009-2016 microchip technology inc. 22.4 brown-out reset (bor) pic18(l)f1xk22 devices implement a bor circuit that provides the user with a number of configuration and power-saving options. the bor is controlled by the borv<1:0> and boren<1:0> bits of the config2l configuration register. there are a total of four bor configurations which are summarized in table 22-1 . the bor threshold is set by the borv<1:0> bits. if bor is enabled (any values of boren<1:0>, except 00 ), any drop of v dd below v bor for greater than t bor will reset the device. a reset may or may not occur if v dd falls below v bor for less than t bor . the chip will remain in brown-out reset until v dd rises above v bor . if the power-up timer is enabled, it will be invoked after v dd rises above v bor ; it then will keep the chip in reset for an additional time delay, t pwrt . if v dd drops below v bor while the power-up timer is running, the chip will go back into a brown-out reset and the power-up timer will be initialized. once v dd rises above v bor , the power-up timer will execute the additional time delay. bor and the power-on timer (pwrt) are independently configured. enabling bor reset does not automatically enable the pwrt. 22.4.1 software enabled bor when boren<1:0> = 01 , the bor can be enabled or disabled by the user in software. this is done with the sboren control bit of the rcon register. setting sboren enables the bor to function as previously described. clearing sboren disables the bor entirely. the sboren bit operates only in this mode; otherwise it is read as 0 . placing the bor under software control gives the user the additional flexibility of tailoring the application to its environment without having to reprogram the device to change bor configuration. it also allows the user to tailor device power consumption in software by eliminating the incremental current that the bor consumes. while the bor current is typically very small, it may have some impact in low-power applications. 22.4.2 detecting bor when bor is enabled, the bor bit always resets to 0 on any bor or por event. this makes it difficult to determine if a bor event has occurred just by reading the state of bor alone. a more reliable method is to simultaneously check the state of both por and bor . this assumes that the por and bor bits are reset to 1 by software immediately after any por event. if bor is 0 while por is 1 , it can be reliably assumed that a bor event has occurred. 22.4.3 disabling bor in sleep mode when boren<1:0> = 10 , the bor remains under hardware control and operates as previously described. whenever the device enters sleep mode, however, the bor is automatically disabled. when the device returns to any other operating mode, bor is automatically re-enabled. this mode allows for applications to recover from brown-out situations, while actively executing code, when the device requires bor protection the most. at the same time, it saves additional power in sleep mode by eliminating the small incremental bor current. table 22-1: bor configurations note: even when bor is under software control, the bor reset voltage level is still set by the borv<1:0> configuration bits. it cannot be changed by software. bor configuration status of sboren (rcon<6>) bor operation boren1 boren0 00 unavailable bor disabled; must be enabled by reprogramming the configuration bits. 01 available bor enabled by software; operation controlled by sboren. 10 unavailable bor enabled by hardware in run and idle modes, disabled during sleep mode. 11 unavailable bor enabled by hardware; must be disabled by reprogramming the configuration bits. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 241 pic18(l)f1xk22 22.5 device reset timers pic18(l)f1xk22 devices incorporate three separate on-chip timers that help regulate the power-on reset process. their main function is to ensure that the device clock is stable before code is executed. these timers are: power-up timer (pwrt) oscillator start-up timer (ost) pll lock time-out 22.5.1 power-up timer (pwrt) the power-up timer (pwrt) of pic18(l)f1xk22 devices is an 11-bit counter which uses the lfintosc source as the clock input. this yields an approximate time interval of 2048 x 32 ? s=65.6ms. while the pwrt is counting, the device is held in reset. the power-up time delay depends on the lfintosc clock and will vary from chip-to-chip due to temperature and process variation. see section 26.0 ?electrical specifications? for details. the pwrt is enabled by clearing the pwrten configuration bit. 22.5.2 oscillator start-up timer (ost) the oscillator start-up timer (ost) provides a 1024 oscillator cycle (from osc1 input) delay after the pwrt delay is over. this ensures that the crystal oscillator or resonator has started and stabilized. the ost time-out is invoked only for xt, lp, hs and hspll modes and only on power-on reset, or on exit from all power-managed modes that stop the external oscillator. 22.5.3 pll lock time-out with the pll enabled in its pll mode, the time-out sequence following a power-on reset is slightly different from other oscillator modes. a separate timer is used to provide a fixed time-out that is sufficient for the pll to lock to the main oscillator frequency. this pll lock time-out (t pll ) is typically 2 ms and follows the oscillator start-up time-out. 22.5.4 time-out sequence on power-up, the time-out sequence is as follows: 1. after the por pulse has cleared, pwrt time-out is invoked (if enabled). 2. then, the ost is activated. the total time-out will vary based on oscillator configuration and the status of the pwrt. figure 22-3 , figure 22-4 , figure 22-5 , figure 22-6 and figure 22-7 all depict time-out sequences on power-up, with the power-up timer enabled and the device operating in hs oscillator mode. figures 22-3 through 22-6 also apply to devices operating in xt or lp modes. for devices in rc mode and with the pwrt disabled, on the other hand, there will be no time-out at all. since the time-outs occur from the por pulse, if mclr is kept low long enough, all time-outs will expire, after which, bringing mclr high will allow program execution to begin immediately ( figure 22-5 ). this is useful for testing purposes or to synchronize more than one pic18(l)f1xk22 device operating in parallel. table 22-2: time-out in various situations oscillator configuration power-up (2) and brown-out exit from power-managed mode pwrten = 0 pwrten = 1 hspll 66 ms (1) + 1024 t osc + 2 ms (2) 1024 t osc + 2 ms (2) 1024 t osc + 2 ms (2) hs, xt, lp 66 ms (1) + 1024 t osc 1024 t osc 1024 t osc ec, ecio 66 ms (1) rc, rcio 66 ms (1) intio1, intio2 66 ms (1) note 1: 66 ms (65.5 ms) is the nominal power-up timer (pwrt) delay. 2: 2 ms is the nominal time required for the pll to lock. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 242 ? 2009-2016 microchip technology inc. figure 22-3: time-out sequ ence on power-up (mclr tied to v dd , v dd rise < t pwrt ) figure 22-4: time-out sequ ence on power-up (mclr not tied to v dd ): case 1 figure 22-5: time-out sequ ence on power-up (mclr not tied to v dd ): case 2 t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset v dd mclr internal por pwrt time-out ost time-out internal reset t pwrt t ost downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 243 pic18(l)f1xk22 figure 22-6: slow rise time (mclr tied to v dd , v dd rise > t pwrt ) figure 22-7: time-out sequence on por w/pll enabled (mclr tied to v dd ) v dd mclr internal por pwrt time-out ost time-out internal reset 0v 5v t pwrt t ost t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset pll time-out t pll note: t ost = 1024 clock cycles. t pll ? 2 ms max. first three stages of the pwrt timer. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 244 ? 2009-2016 microchip technology inc. 22.6 reset state of registers some registers are unaffected by a reset. their status is unknown on por and unchanged by all other resets. all other registers are forced to a reset state depending on the type of reset that occurred. most registers are not affected by a wdt wake-up, since this is viewed as the resumption of normal operation. status bits from the rcon register, ri , to , pd , por and bor , are set or cleared differently in different reset situations, as indicated in tab l e 2 2- 3 . these bits are used by software to determine the nature of the reset. table 22-4 describes the reset states for all of the special function registers. these are categorized by power-on and brown-out resets, master clear and wdt resets and wdt wake-ups. table 22-3: status bits, their signifi cance and the initialization condition for rcon register condition program counter rcon register stkptr register sboren ri to pd por bor stkovf stkunf power-on reset 0000h 1 11100 0 0 reset instruction 0000h u (2) 0uuuu u u brown-out reset 0000h u (2) 111u0 u u mclr during power-managed run modes 0000h u (2) u1uuu u u mclr during power-managed idle modes and sleep mode 0000h u (2) u10uu u u wdt time-out during full power or power-managed run mode 0000h u (2) u0uuu u u mclr during full power execution 0000h u (2) uuuuu u u stack full reset (stvren = 1 ) 0000h u (2) uuuuu 1 u stack underflow reset (stvren = 1 ) 0000h u (2) uuuuu u 1 stack underflow error (not an actual reset, stvren = 0 ) 0000h u (2) uuuuu u 1 wdt time-out during power-managed idle or sleep modes pc + 2 u (2) u00uu u u interrupt exit from power-managed modes pc + 2 (1) u (2) uu0uu u u legend: u = unchanged note 1: when the wake-up is due to an interrupt and the gieh or giel bits are set, the pc is loaded with the interrupt vector (008h or 0018h). 2: reset state is 1 for por and unchanged for all other resets when software bor is enabled (boren<1:0> configuration bits = 01 and sboren = 1 ). otherwise, the reset state is 0 . downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 245 pic18(l)f1xk22 table 22-4: initialization conditions for all registers register address power-on reset, brown-out reset mclr resets, wdt reset, reset instruction, stack resets wake-up via wdt or interrupt tosu fffh ---0 0000 ---0 0000 ---0 uuuu (3) tosh ffeh 0000 0000 0000 0000 uuuu uuuu (3) tosl ffdh 0000 0000 0000 0000 uuuu uuuu (3) stkptr ffch 00-0 0000 uu-0 0000 uu-u uuuu (3) pclatu ffbh ---0 0000 ---0 0000 ---u uuuu pclath ffah 0000 0000 0000 0000 uuuu uuuu pcl ff9h 0000 0000 0000 0000 pc + 2 (2) tblptru ff8h ---0 0000 ---0 0000 ---u uuuu tblptrh ff7h 0000 0000 0000 0000 uuuu uuuu tblptrl ff6h 0000 0000 0000 0000 uuuu uuuu tablat ff5h 0000 0000 0000 0000 uuuu uuuu prodh ff4h xxxx xxxx uuuu uuuu uuuu uuuu prodl ff3h xxxx xxxx uuuu uuuu uuuu uuuu intcon ff2h 0000 000x 0000 000u uuuu uuuu (1) intcon2 ff1h 1111 -1-1 1111 -1-1 uuuu -u-u (1) intcon3 ff0h 11-0 0-00 11-0 0-00 uu-u u-uu (1) indf0 fefh n/a n/a n/a postinc0 feeh n/a n/a n/a postdec0 fedh n/a n/a n/a preinc0 fech n/a n/a n/a plusw0 febh n/a n/a n/a fsr0h feah ---- 0000 ---- 0000 ---- uuuu fsr0l fe9h xxxx xxxx uuuu uuuu uuuu uuuu wreg fe8h xxxx xxxx uuuu uuuu uuuu uuuu indf1 fe7h n/a n/a n/a postinc1 fe6h n/a n/a n/a postdec1 fe5h n/a n/a n/a preinc1 fe4h n/a n/a n/a plusw1 fe3h n/a n/a n/a legend: u = unchanged, x = unknown, - = unimplemented bit, read as 0 , q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu , tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in t he hardware stack. 4: see table 22-3 for reset value for specific condition. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 246 ? 2009-2016 microchip technology inc. fsr1h fe2h ---- 0000 ---- 0000 ---- uuuu fsr1l fe1h xxxx xxxx uuuu uuuu uuuu uuuu bsr fe0h ---- 0000 ---- 0000 ---- uuuu indf2 fdfh n/a n/a n/a postinc2 fdeh n/a n/a n/a postdec2 fddh n/a n/a n/a preinc2 fdch n/a n/a n/a plusw2 fdbh n/a n/a n/a fsr2h fdah ---- 0000 ---- 0000 ---- uuuu fsr2l fd9h xxxx xxxx uuuu uuuu uuuu uuuu status fd8h ---x xxxx ---u uuuu ---u uuuu tmr0h fd7h 0000 0000 0000 0000 uuuu uuuu tmr0l fd6h xxxx xxxx uuuu uuuu uuuu uuuu t0con fd5h 1111 1111 1111 1111 uuuu uuuu osccon fd3h 0011 qq00 0011 qq00 uuuu uuuu osccon2 fd2h ---- -10x ---- -10x ---- -uuu wdtcon fd1h ---- ---0 ---- ---0 ---- ---u rcon (4) fd0h 0q-1 11q0 0q-q qquu uq-u qquu tmr1h fcfh xxxx xxxx uuuu uuuu uuuu uuuu tmr1l fceh xxxx xxxx uuuu uuuu uuuu uuuu t1con fcdh 0000 0000 u0uu uuuu uuuu uuuu tmr2 fcch 0000 0000 0000 0000 uuuu uuuu pr2 fcbh 1111 1111 1111 1111 1111 1111 t2con fcah -000 0000 -000 0000 -uuu uuuu sspbuf fc9h xxxx xxxx uuuu uuuu uuuu uuuu sspadd fc8h 0000 0000 0000 0000 uuuu uuuu sspstat fc7h 0000 0000 0000 0000 uuuu uuuu sspcon1 fc6h 0000 0000 0000 0000 uuuu uuuu sspcon2 fc5h 0000 0000 0000 0000 uuuu uuuu table 22-4: initialization conditions for all registers (continued) register address power-on reset, brown-out reset mclr resets, wdt reset, reset instruction, stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as 0 , q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu , tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in t he hardware stack. 4: see table 22-3 for reset value for specific condition. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 247 pic18(l)f1xk22 adresh fc4h xxxx xxxx uuuu uuuu uuuu uuuu adresl fc3h xxxx xxxx uuuu uuuu uuuu uuuu adcon0 fc2h --00 0000 --00 0000 --uu uuuu adcon1 fc1h ---- 0000 ---- 0000 ---- uuuu adcon2 fc0h 0-00 0000 0-00 0000 u-uu uuuu ccpr1h fbfh xxxx xxxx uuuu uuuu uuuu uuuu ccpr1l fbeh xxxx xxxx uuuu uuuu uuuu uuuu ccp1con fbdh 0000 0000 0000 0000 uuuu uuuu vrefcon2 fbch ---0 0000 ---0 0000 ---u uuuu vrefcon1 fbbh 000- 00-0 000- 00-0 uuu- uu-u vrefcon0 fbah 0001 ---- 0001 ---- uuuu ---- pstrcon fb9h ---0 0001 ---0 0001 ---u uuuu baudcon fb8h 0100 0-00 0100 0-00 uuuu u-uu pwm1con fb7h 0000 0000 0000 0000 uuuu uuuu eccp1as fb6h 0000 0000 0000 0000 uuuu uuuu tmr3h fb3h xxxx xxxx uuuu uuuu uuuu uuuu tmr3l fb2h xxxx xxxx uuuu uuuu uuuu uuuu t3con fb1h 0-00 0000 u-uu uuuu u-uu uuuu spbrgh fb0h 0000 0000 0000 0000 uuuu uuuu spbrg fafh 0000 0000 0000 0000 uuuu uuuu rcreg faeh 0000 0000 0000 0000 uuuu uuuu txreg fadh 0000 0000 0000 0000 uuuu uuuu txsta fach 0000 0010 0000 0010 uuuu uuuu rcsta fabh 0000 000x 0000 000x uuuu uuuu eeadr faah 0000 0000 0000 0000 uuuu uuuu eedata fa8h 0000 0000 0000 0000 uuuu uuuu eecon2 fa7h 0000 0000 0000 0000 0000 0000 eecon1 fa6h xx-0 x000 uu-0 u000 uu-0 u000 table 22-4: initialization conditions for all registers (continued) register address power-on reset, brown-out reset mclr resets, wdt reset, reset instruction, stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as 0 , q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu , tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in t he hardware stack. 4: see table 22-3 for reset value for specific condition. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 248 ? 2009-2016 microchip technology inc. ipr2 fa2h 1111 1-1- 1111 1-1- uuuu u-u- pir2 fa1h 0000 0-0- 0000 0-0- uuuu u-u- (1) pie2 fa0h 0000 0-0- 0000 0-0- uuuu u-u- ipr1 f9fh -111 1111 -111 1111 -uuu uuuu pir1 f9eh -000 0000 -000 0000 -uuu uuuu (1) pie1 f9dh -000 0000 -000 0000 -uuu uuuu osctune f9bh 0000 0000 0000 0000 uuuu uuuu trisc f95h 1111 1111 1111 1111 uuuu uuuu trisb f94h 1111 ---- 1111 ---- uuuu ---- trisa f93h --11 1111 --11 1111 --uu uuuu latc f8bh xxxx xxxx uuuu uuuu uuuu uuuu latb f8ah xxxx ---- uuuu ---- uuuu ---- lata f89h --xx xxxx --uu uuuu --uu uuuu portc f82h xxxx xxxx uuuu uuuu uuuu uuuu portb f81h xxxx ---- uuuu ---- uuuu ---- porta f80h --xx xxxx --xx xxxx --uu uuuu anselh f7fh ---- 1111 ---- 1111 ---- uuuu ansel f7eh 1111 1111 1111 1111 uuuu uuuu iocb f7ah 0000 ---- 0000 ---- uuuu ---- ioca f79h --00 0000 --00 0000 --uu uuuu wpub f78h 1111 ---- 1111 ---- uuuu ---- wpua f77h --11 1111 --11 1111 --uu uuuu slrcon f76h ---- -111 ---- -111 ---- -uuu sspmsk f6fh 1111 1111 1111 1111 uuuu uuuu cm1con0 f6dh 0000 0000 0000 0000 uuuu uuuu cm2con1 f6ch 0000 0000 0000 0000 uuuu uuuu cm2con0 f6bh 0000 0000 0000 0000 uuuu uuuu srcon1 f69h 0000 0000 0000 0000 uuuu uuuu srcon0 f68h 0000 0000 0000 0000 uuuu uuuu table 22-4: initialization conditions for all registers (continued) register address power-on reset, brown-out reset mclr resets, wdt reset, reset instruction, stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as 0 , q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu , tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in t he hardware stack. 4: see table 22-3 for reset value for specific condition. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 249 pic18(l)f1xk22 23.0 special features of the cpu pic18(l)f1xk22 devices include several features intended to maximize reliability and minimize cost through elimination of external components. these are: oscillator selection resets: - power-on reset (por) - power-up timer (pwrt) - oscillator start-up timer (ost) - brown-out reset (bor) interrupts watchdog timer (wdt) code protection id locations in-circuit serial programming? the oscillator can be configured for the application depending on frequency, power, accuracy and cost. all of the options are discussed in detail in section 2.0 ?oscillator module? . a complete discussion of device resets and interrupts is available in previous sections of this data sheet. in addition to their power-up and oscillator start-up timers provided for resets, pic18(l)f1xk22 devices have a watchdog timer, which is either permanently enabled via the configuration bits or software controlled (if configured as disabled). the inclusion of an internal rc oscillator also provides the additional benefits of a fail-safe clock monitor (fscm) and two-speed start-up. fscm provides for background monitoring of the peripheral clock and automatic switchover in the event of its failure. two- speed start-up enables code to be executed almost immediately on start-up, while the primary clock source completes its start-up delays. all of these features are enabled and configured by setting the appropriate configuration register bits. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 250 ? 2009-2016 microchip technology inc. 23.1 configuration bits the configuration bits can be programmed (read as 0 ) or left unprogrammed (read as 1 ) to select various device configurations. these bits are mapped starting at program memory location 300000h. the user will note that address 300000h is beyond the user program memory space. in fact, it belongs to the configuration memory space (300000h-3fffffh), which can only be accessed using table reads and table writes. programming the configuration registers is done in a manner similar to programming the flash memory. the wr bit in the eecon1 register starts a self-timed write to the configuration register. in normal operation mode, a tblwt instruction with the tblptr pointing to the configuration register sets up the address and the data for the configuration register write. setting the wr bit starts a long write to the configuration register. the configuration registers are written a byte at a time. to write or erase a configuration cell, a tblwt instruction can write a 1 or a 0 into the cell. for additional details on flash programming, refer to section 4.5 ?writing to flash program memory? . table 23-1: configuration bits and device ids file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 default/ unprogrammed value 300001h config1h ieso fcmen pclken pll_en fosc3 fosc2 fosc1 fosc0 0010 0111 300002h config2l borv1 borv0 boren1 boren0 pwrten ---1 1111 300003h config2h wdtps3 wdtps2 wdtps1 wdtps0 wdten ---1 1111 300005h config3h mclre hfofst 1--- 1--- 300006h config4l bkbug enhcpu b b s i zl v p s t v r e n -0-- 01-1 300008h config5l c p 1c p 0 ---- --11 300009h config5h cpd cpb 11-- ---- 30000ah config6l wrt1 wrt0 ---- --11 30000bh config6h wrtd wrtb wrtc 111- ---- 30000ch config7l ebtr1 ebtr0 ---- --11 30000dh config7h e b t r b -1-- ---- 3ffffeh devid1 (1) dev2 dev1 dev0 rev4 rev3 rev2 rev1 rev0 qqqq qqqq (1) 3fffffh devid2 (1) dev10 dev9 dev8 dev7 dev6 dev5 dev4 dev3 0000 1100 legend: x = unknown, u = unchanged, C = unimplemented, q = value depends on condition. shaded cells are unimplemented, read as 0 note 1: see register 23-12 for devid1 values. devid registers are read-only and cannot be programmed by the user. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 251 pic18(l)f1xk22 register 23-1: config1h: co nfiguration register 1 high r/p-0 r/p-0 r/p-1 r/p-0 r/p-0 r/p-1 r/p-1 r/p-1 ieso fcmen pclken pll_en fosc3 fosc2 fosc1 fosc0 bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as 0 -n = value when device is unprogrammed x = bit is unknown bit 7 ieso: internal/external oscillator switchover bit 1 = oscillator switchover mode enabled 0 = oscillator switchover mode disabled bit 6 fcmen: fail-safe clock monitor enable bit 1 = fail-safe clock monitor enabled 0 = fail-safe clock monitor disabled bit 5 pclken: primary clock enable bit 1 = primary clock enabled 0 = primary clock is under software control bit 4 pll_en: 4 x pll enable bit 1 = oscillator multiplied by 4 0 = pll is under software control bit 3-0 fosc<3:0>: oscillator selection bits 1111 = external rc oscillator, clkout function on osc2 1110 = external rc oscillator, clkout function on osc2 1101 = ec (low) 1100 = ec, clkout function on osc2 (low) 1011 = ec (medium) 1010 = ec, clkout function on osc2 (medium) 1001 = internal rc oscillator, clkout function on osc2 1000 = internal rc oscillator 0111 = external rc oscillator 0110 = external rc oscillator, clkout function on osc2 0101 =ec (high) 0100 = ec, clkout function on osc2 (high) 0011 = external rc oscillator, clkout function on osc2 0010 = hs oscillator 0001 = xt oscillator 0000 = lp oscillator downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 252 ? 2009-2016 microchip technology inc. register 23-2: config2l: co nfiguration register 2 low u-0 u-0 u-0 r/p-1 r/p-1 r/p-1 r/p-1 r/p-1 b o r v 1 (1) borv0 (1) boren1 (2) boren0 (2) pwrten (2) bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as 0 -n = value when device is unprogrammed x = bit is unknown bit 7-5 unimplemented: read as 0 bit 4-3 borv<1:0>: brown-out reset voltage bits (1) 11 = v bor set to 1.9v nominal 10 = v bor set to 2.2v nominal 01 = v bor set to 2.5v nominal 00 = v bor set to 2.85v nominal bit 2-1 boren<1:0>: brown-out reset enable bits (2) 11 = brown-out reset enabled in hardware only (sboren is disabled) 10 = brown-out reset enabled in hardware only and disabled in sleep mode (sboren is disabled) 01 = brown-out reset enabled and controlled by software (sboren is enabled) 00 = brown-out reset disabled in hardware and software bit 0 pwrten : power-up timer enable bit (2) 1 = pwrt disabled 0 = pwrt enabled note 1: see table 26-1 for specifications. 2: the power-up timer is decoupled from brown-out reset, allowing these features to be independently controlled. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 253 pic18(l)f1xk22 register 23-3: config2h: co nfiguration register 2 high u-0 u-0 u-0 r/p-1 r/p-1 r/p-1 r/p-1 r/p-1 wdtps3 wdtps2 wdtps1 wdtps0 wdten bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as 0 -n = value when device is unprogrammed x = bit is unknown bit 7-5 unimplemented: read as 0 bit 4-1 wdtps<3:0>: watchdog timer postscale select bits 1111 = 1:32,768 1110 = 1:16,384 1101 = 1:8,192 1100 = 1:4,096 1011 = 1:2,048 1010 = 1:1,024 1001 = 1:512 1000 = 1:256 0111 = 1:128 0110 = 1:64 0101 = 1:32 0100 = 1:16 0011 = 1:8 0010 = 1:4 0001 = 1:2 0000 = 1:1 bit 0 wdten: watchdog timer enable bit 1 = wdt is always enabled. swdten bit has no effect 0 = wdt is controlled by swdten bit of the wdtcon register downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 254 ? 2009-2016 microchip technology inc. register 23-4: config3h: co nfiguration register 3 high r/p-1 u-0 u-0 u-0 r/p-1 u-0 u-0 u-0 mclre hfofst bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as 0 -n = value when device is unprogrammed x = bit is unknown bit 7 mclre: mclr pin enable bit 1 = mclr pin enabled; ra3 input pin disabled 0 = ra3 input pin enabled; mclr disabled bit 6-4 unimplemented: read as 0 bit 3 hfofst: hfintosc fast start-up bit 1 = hfintosc starts clocking the cpu without waiting for the oscillator to stabilize. 0 = the system clock is held off until the hfintosc is stable. bit 2-0 unimplemented: read as 0 register 23-5: config4l: co nfiguration register 4 low r/w-1 (1) r/w-0 u-0 u-0 r/p-0 r/p-1 u-0 r/p-1 bkbug enhcpu bbsiz lvp s t v r e n bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as 0 -n = value when device is unprogrammed x = bit is unknown bit 7 bkbug : background debugger enable bit (1) 1 = background debugger disabled 0 = background debugger functions enabled bit 6 enhcpu: enhanced cpu enable bit 1 = enhanced cpu enabled 0 = enhanced cpu disabled bit 5-4 unimplemented: read as 0 bit 3 bbsiz: boot block size select bit 1 = 2 kw boot block size for pic18(l)f14k22 (1 kw boot block size for pic18(l)f13k22) 0 = 1 kw boot block size for pic18(l)f14k22 (512 w boot block size for pic18(l)f13k22) bit 2 lvp: single-supply icsp? enable bit 1 = single-supply icsp enabled 0 = single-supply icsp disabled bit 1 unimplemented: read as 0 bit 0 stvren: stack full/underflow reset enable bit 1 = stack full/underflow will cause reset 0 = stack full/underflow will not cause reset note 1: bkbug is only used for icd device. otherwise, this bit is unimplemented and reads as 1. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 255 pic18(l)f1xk22 register 23-6: config5l: co nfiguration register 5 low u-0 u-0 u-0 u-0 u-0 u-0 r/c-1 r/c-1 c p 1c p 0 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as 0 -n = value when device is unprogrammed c = clearable only bit bit 7-2 unimplemented: read as 0 bit 1 cp1: code protection bit 1 = block 1 not code-protected 0 = block 1 code-protected bit 0 cp0: code protection bit 1 = block 0 not code-protected 0 = block 0 code-protected register 23-7: config5h: co nfiguration register 5 high r/c-1 r/c-1 u-0 u-0 u-0 u-0 u-0 u-0 cpd cpb bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as 0 -n = value when device is unprogrammed c = clearable only bit bit 7 cpd: data eeprom code protection bit 1 = data eeprom not code-protected 0 = data eeprom code-protected bit 6 cpb: boot block code protection bit 1 = boot block not code-protected 0 = boot block code-protected bit 5-0 unimplemented: read as 0 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 256 ? 2009-2016 microchip technology inc. register 23-8: config6l: co nfiguration register 6 low u-0 u-0 u-0 u-0 u-0 u-0 r/c-1 r/c-1 w r t 1w r t 0 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as 0 -n = value when device is unprogrammed c = clearable only bit bit 7-2 unimplemented: read as 0 bit 1 wrt1: write protection bit 1 = block 1 not write-protected 0 = block 1 write-protected bit 0 wrt0: write protection bit 1 = block 0 not write-protected 0 = block 0 write-protected register 23-9: config6h: co nfiguration register 6 high r/c-1 r/c-1 r-1 u-0 u-0 u-0 u-0 u-0 wrtd wrtb wrtc (1) bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as 0 -n = value when device is unprogrammed c = clearable only bit bit 7 wrtd: data eeprom write protection bit 1 = data eeprom not write-protected 0 = data eeprom write-protected bit 6 wrtb: boot block write protection bit 1 = boot block not write-protected 0 = boot block write-protected bit 5 wrtc: configuration register write protection bit (1) 1 = configuration registers not write-protected 0 = configuration registers write-protected bit 4-0 unimplemented: read as 0 note 1: this bit is read-only in normal execution mode; it can be written only in program mode. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 257 pic18(l)f1xk22 register 23-10: config7l: co nfiguration register 7 low u-0 u-0 u-0 u-0 u-0 u-0 r/c-1 r/c-1 ebtr1 ebtr0 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as 0 -n = value when device is unprogrammed c = clearable only bit bit 7-2 unimplemented: read as 0 bit 1 ebtr1: table read protection bit 1 = block 1 not protected from table reads executed in other blocks 0 = block 1 protected from table reads executed in other blocks bit 0 ebtr0: table read protection bit 1 = block 0 not protected from table reads executed in other blocks 0 = block 0 protected from table reads executed in other blocks register 23-11: config7h: co nfiguration register 7 high u-0 r/c-1 u-0 u-0 u-0 u-0 u-0 u-0 ebtrb bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as 0 -n = value when device is unprogrammed c = clearable only bit bit 7 unimplemented: read as 0 bit 6 ebtrb: boot block table read protection bit 1 = boot block not protected from table reads executed in other blocks 0 = boot block protected from table reads executed in other blocks bit 5-0 unimplemented: read as 0 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 258 ? 2009-2016 microchip technology inc. register 23-12: devid1: device id register 1 for pic18(l)f1xk22 rrrrrrrr dev2 dev1 dev0 rev4 rev3 rev2 rev1 rev0 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as 0 -n = value when device is unprogrammed c = clearable only bit bit 7-5 dev<2:0>: device id bits 010 = pic18(l)f13k22 011 = pic18(l)f14k22 bit 4-0 rev<4:0>: revision id bits these bits are used to indicate the device revision. register 23-13: devid2: device id register 2 for pic18(l)f1xk22 rrrrrrrr dev10 dev9 dev8 dev7 dev6 dev5 dev4 dev3 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as 0 -n = value when device is unprogrammed c = clearable only bit bit 7-0 dev<10:3>: device id bits these bits are used with the dev<2:0> bits in the device id register 1 to identify the part number. 0010 0000 = pic18f13k22/PIC18F14K22 devices (1) note 1: these values for dev<10:3> may be shared with other devices. the specific device is always identified by using the entire dev<10:0> bit sequence. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 259 pic18(l)f1xk22 23.2 watchdog timer (wdt) for pic18(l)f1xk22 devices, the wdt is driven by the lfintosc source. when the wdt is enabled, the clock source is also enabled. the nominal wdt period is 4 ms and has the same stability as the lfintosc oscillator. the 4-millisecond period of the wdt is multiplied by a 16-bit postscaler. any output of the wdt postscaler is selected by a multiplexer, controlled by bits in configuration register 2h. available periods range from 4 ms to 131.072 seconds (2.18 minutes). the wdt and postscaler are cleared when any of the following events occur: a sleep or clrwdt instruction is executed, the ircf bits of the osccon register are changed or a clock failure has occurred. figure 23-1: wdt block diagram note 1: the clrwdt and sleep instructions clear the wdt and postscaler counts when executed. 2: changing the setting of the ircf bits of the osccon register clears the wdt and postscaler counts. lfintosc source wdt wake-up reset wdt counter programmable postscaler 1:1 to 1:32,768 enable wdt wdtps<3:0> swdten wdten clrwdt 4 from power reset all device resets sleep ? 128 change on ircf bits managed modes downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 260 ? 2009-2016 microchip technology inc. 23.2.1 control register register 23-14 shows the wdtcon register. this is a readable and writable register which contains a control bit that allows software to override the wdt enable configuration bit, but only if the configuration bit has disabled the wdt. table 23-2: summary of watchdog timer registers 23.3 program verification and code protection the overall structure of the code protection on the pic18 flash devices differs significantly from other pic microcontroller devices. the user program memory is divided into five blocks. one of these is a boot block of 0.5k or 2k bytes, depending on the device. the remainder of the memory is divided into individual blocks on binary boundaries. each of the five blocks has three code protection bits associated with them. they are: code-protect bit (cpn) write-protect bit (wrtn) external block table read bit (ebtrn) figure 23-2 shows the program memory organization for 8, 16 and 32-kbyte devices and the specific code protection bit associated with each block. the actual locations of the bits are summarized in table 23-3 . register 23-14: wdtcon: wat chdog timer control register u-0 u-0 u-0 u-0 u-0 u-0 u-0 r/w-0 s w d t e n (1) bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 -n = value at por 1 = bit is set 0 = bit is cleared x = bit is unknown bit 7-1 unimplemented: read as 0 bit 0 swdten: software enable or disable the watchdog timer bit (1) 1 = wdt is turned on 0 = wdt is turned off (reset value) note 1: this bit has no effect if the configuration bit, wdten, is enabled. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page config2h wdtps3 wdtps2 wdtps1 wdtps0 wdten 253 rcon ipen sboren ri to pd por bor 246 wdtcon s w d t e n 246 legend: = unimplemented, read as 0 . shaded cells are not used by the watchdog timer. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 261 pic18(l)f1xk22 figure 23-2: code-protected prog ram memory for pic18(l)f1xk22 device address (from/to) 14k22 13k22 bbsiz = 1 bbsiz = 0 bbsiz = 1 bbsiz = 0 0000h 03ffh boot block, 4 kb cpb, wrtb, ebtrb boot block, 2 kb cpb, wrtb, ebtrb boot block, 2 kb cpb, wrtb, ebtrb boot block, 1 kb cpb, wrtb, ebtrb 0400h 07ffh block 0 1.512 kb cp0, wrt0, ebtr0 0800h 0bffh block 0 6 kb cp0, wrt0, ebtr0 block 0 2 kb cp0, wrt0, ebtr0 0c00h 0fffh 1000h 1fffh block 0 4 kb cp0, wrt0, ebtr0 block 1 4 kb cp1, wrt1, ebtr1 block 1 4 kb cp1, wrt1, ebtr1 2000h 3fffh block 1 8 kb cp1, wrt1, ebtr1 block 1 8 kb cp1, wrt1, ebtr1 reads all 0 s reads all 0 s 4000h 4ffeh reads all 0 s reads all 0 s 5000h 5ffeh 6000h 6ffeh 7000h 7ffeh 8000h 8ffeh 9000h 9ffeh a000h affeh b000h bffeh c000h cffeh d000h dffeh e000h effeh f000h fffeh h000h hffeh note: refer to the test section for requirements on test memory mapping. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 262 ? 2009-2016 microchip technology inc. table 23-3: summary of code protection registers 23.3.1 program memory code protection the program memory may be read to or written from any location using the table read and table write instructions. the device id may be read with table reads. the configuration registers may be read and written with the table read and table write instructions. in normal execution mode, the cpn bits have no direct effect. cpn bits inhibit external reads and writes. a block of user memory may be protected from table writes if the wrtn configuration bit is 0 . the ebtrn bits control table reads. for a block of user memory with the ebtrn bit cleared to 0 , a table read instruction that executes from within that block is allowed to read. a table read instruction that executes from a location outside of that block is not allowed to read and will result in reading 0 s. figures 23-3 through 23-5 illustrate table write and table read protection. figure 23-3: table write (wrtn) disallowed file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 300008h config5l c p 1c p 0 300009h config5h cpd cpb 30000ah config6l wrt1 wrt0 30000bh config6h wrtd wrtb wrtc (1) 30000ch config7l ebtr1 ebtr0 30000dh config7h ebtrb legend: shaded cells are unimplemented. note 1: unimplemented in pic18f13k22 and PIC18F14K22 devices; maintain this bit set. note: code protection bits may only be written to a 0 from a 1 state. it is not possible to write a 1 to a bit in the 0 state. code protection bits are only set to 1 by a full chip erase or block erase function. the full chip erase and block erase functions can only be initiated via icsp or an external programmer. 000000h 0007ffh 000800h 001fffh 002000h 003fffh 004000h 005fffh 006000h 007fffh wrtb, ebtrb = 11 wrt0, ebtr0 = 01 wrt1, ebtr1 = 11 wrt2, ebtr2 = 11 wrt3, ebtr3 = 11 tblwt* tblptr = 0008ffh pc = 001ffeh tblwt* pc = 005ffeh register values program memory configuration bit settings results: all table writes disabled to blockn whenever wrtn = 0 . downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 263 pic18(l)f1xk22 figure 23-4: external block table read (ebtrn) disallowed figure 23-5: external block table read (ebtrn) allowed wrtb, ebtrb = 11 wrt0, ebtr0 = 10 wrt1, ebtr1 = 11 wrt2, ebtr2 = 11 wrt3, ebtr3 = 11 tblrd* tblptr = 0008ffh pc = 003ffeh results: all table reads from external blocks to blockn are disabled whenever ebtrn = 0 . tablat register returns a value of 0 . register values program memory configuration bit settings 000000h 0007ffh 000800h 001fffh 002000h 003fffh 004000h 005fffh 006000h 007fffh wrtb, ebtrb = 11 wrt0, ebtr0 = 10 wrt1, ebtr1 = 11 wrt2, ebtr2 = 11 wrt3, ebtr3 = 11 tblrd* tblptr = 0008ffh pc = 001ffeh register values program memory configuration bit settings results: table reads permitted within blockn, even when ebtrbn = 0 . tablat register returns the value of the data at the location tblptr. 000000h 0007ffh 000800h 001fffh 002000h 003fffh 004000h 005fffh 006000h 007fffh downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 264 ? 2009-2016 microchip technology inc. 23.3.2 data eeprom code protection the entire data eeprom is protected from external reads and writes by two bits: cpd and wrtd. cpd inhibits external reads and writes of data eeprom. wrtd inhibits internal and external writes to data eeprom. the cpu can always read data eeprom under normal operation, regardless of the protection bit settings. 23.3.3 configuration register protection the configuration registers can be write-protected. the wrtc bit controls protection of the configuration registers. in normal execution mode, the wrtc bit is readable only. wrtc can only be written via icsp or an external programmer. 23.4 id locations eight memory locations (200000h-200007h) are designated as id locations, where the user can store checksum or other code identification numbers. these locations are both readable and writable during normal execution through the tblrd and tblwt instructions or during program/verify. the id locations can be read when the device is code-protected. 23.5 in-circuit serial programming pic18(l)f1xk22 devices can be serially programmed while in the end application circuit. this is simply done with two lines for clock and data and three other lines for power, ground and the programming voltage. this allows customers to manufacture boards with unprogrammed devices and then program the microcontroller just before shipping the product. this also allows the most recent firmware or a custom firmware to be programmed. 23.6 in-circuit debugger when the debug configuration bit is programmed to a 0 , the in-circuit debugger functionality is enabled. this function allows simple debugging functions when used with mplab ? ide. when the microcontroller has this feature enabled, some resources are not available for general use. table 23-4 shows which resources are required by the background debugger. table 23-4: debugger resources to use the in-circuit debugger function of the microcontroller, the design must implement in-circuit serial programming connections to the following pins: mclr /v pp /ra3 v dd v ss ra0 ra1 this will interface to the in-circuit debugger module available from microchip or one of the third party development tool companies. 23.7 single-supply icsp programming the lvp configuration bit enables single-supply icsp programming (formerly known as low-voltage icsp programming or lvp). when single-supply programming is enabled, the microcontroller can be programmed without requiring high voltage being applied to the mclr /v pp /ra3 pin, but the rc3/pgm pin is then dedicated to controlling program mode entry and is not available as a general purpose i/o pin. while programming, using single-supply programming mode, v dd is applied to the mclr /v pp /ra3 pin as in normal execution mode. to enter programming mode, v dd is applied to the pgm pin. if single-supply icsp programming mode will not be used, the lvp bit can be cleared. rc3/pgm then becomes available as the digital i/o pin, rc3. the lvp bit may be set or cleared only when using standard high-voltage programming (v ihh applied to the mclr / v pp /ra3 pin). once lvp has been disabled, only the standard high-voltage programming is available and must be used to program the device. memory that is not code-protected can be erased using either a block erase, or erased row by row, then written at any specified v dd . if code-protected memory is to be erased, a block erase is required. i/o pins: ra0, ra1 stack: 2 levels program memory: 512 bytes data memory: 10 bytes note 1: high-voltage programming is always available, regardless of the state of the lvp bit or the pgm pin, by applying v ihh to the mclr pin. 2: by default, single-supply icsp is enabled in unprogrammed devices (as supplied from microchip) and erased devices. 3: when single-supply programming is enabled, the rc3 pin can no longer be used as a general purpose i/o pin. 4: when lvp is enabled, externally pull the pgm pin to v ss to allow normal program execution. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 265 pic18(l)f1xk22 24.0 instruction set summary pic18(l)f1xk22 devices incorporate the standard set of 75 pic18 core instructions, as well as an extended set of eight new instructions, for the optimization of code that is recursive or that utilizes a software stack. the extended set is discussed later in this section. 24.1 standard instruction set the standard pic18 instruction set adds many enhancements to the previous pic ? mcu instruction sets, while maintaining an easy migration from these pic mcu instruction sets. most instructions are a single program memory word (16 bits), but there are four instructions that require two program memory locations. each single-word instruction is a 16-bit word divided into an opcode, which specifies the instruction type and one or more operands, which further specify the operation of the instruction. the instruction set is highly orthogonal and is grouped into four basic categories: byte-oriented operations bit-oriented operations literal operations control operations the pic18 instruction set summary in table 24-2 lists byte-oriented , bit-oriented , literal and control operations. table 24-1 shows the opcode field descriptions. most byte-oriented instructions have three operands: 1. the file register (specified by f) 2. the destination of the result (specified by d) 3. the accessed memory (specified by a) the file register designator f specifies which file register is to be used by the instruction. the destination designator d specifies where the result of the operation is to be placed. if d is zero, the result is placed in the wreg register. if d is one, the result is placed in the file register specified in the instruction. all bit-oriented instructions have three operands: 1. the file register (specified by f) 2. the bit in the file register (specified by b) 3. the accessed memory (specified by a) the bit field designator b selects the number of the bit affected by the operation, while the file register designator f represents the number of the file in which the bit is located. the literal instructions may use some of the following operands: a literal value to be loaded into a file register (specified by k) the desired fsr register to load the literal value into (specified by f) no operand required (specified by ) the control instructions may use some of the following operands: a program memory address (specified by n) the mode of the call or return instructions (specified by s) the mode of the table read and table write instructions (specified by m) no operand required (specified by ) all instructions are a single word, except for four double-word instructions. these instructions were made double-word to contain the required information in 32 bits. in the second word, the 4 msbs are 1 s. if this second word is executed as an instruction (by itself), it will execute as a nop . all single-word instructions are executed in a single instruction cycle, unless a conditional test is true or the program counter is changed as a result of the instruction. in these cases, the execution takes two instruction cycles, with the additional instruction cycle(s) executed as a nop . the double-word instructions execute in two instruction cycles. one instruction cycle consists of four oscillator periods. thus, for an oscillator frequency of 4 mhz, the normal instruction execution time is 1 ? s. if a conditional test is true, or the program counter is changed as a result of an instruction, the instruction execution time is 2 ? s. two-word branch instructions (if true) would take 3 ? s. figure 24-1 shows the general formats that the instructions can have. all examples use the convention nnh to represent a hexadecimal number. the instruction set summary, shown in ta bl e 2 4 -2 , lists the standard instructions recognized by the microchip assembler (mpasm tm ). section 24.1.1 ?standard instruction set? provides a description of each instruction. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 266 ? 2009-2016 microchip technology inc. table 24-1: opcode field descriptions field description a ram access bit a = 0 : ram location in access ram (bsr register is ignored) a = 1 : ram bank is specified by bsr register bbb bit address within an 8-bit file register (0 to 7). bsr bank select register. used to select the current ram bank. c, dc, z, ov, n alu status bits: c arry, d igit c arry, z ero, ov erflow, n egative. d destination select bit d = 0 : store result in wreg d = 1 : store result in file register f dest destination: either the wreg register or the specified register file location. f 8-bit register file address (00h to ffh) or 2-bit fsr designator (0h to 3h). f s 12-bit register file address (000h to ff fh). this is the source address. f d 12-bit register file address (000h to fffh). this is the destination address. gie global interrupt enable bit. k literal field, constant data or label (may be either an 8-bit, 12-bit or a 20-bit value). label label name. mm the mode of the tblptr register for the table read and table write instructions. only used with table read and table write instructions: * no change to register (such as tb lptr with table reads and writes) *+ post-increment register (such as tblptr with table reads and writes) *- post-decrement register (such as tblptr with table reads and writes) +* pre-increment register (such as tblptr with table reads and writes) n the relative address (2s complement number) for relative branch instructions or the direct address for call / branch and return instructions. pc program counter. pcl program counter low byte. pch program counter high byte. pclath program counter high byte latch. pclatu program counter upper byte latch. pd power-down bit. prodh product of multiply high byte. prodl product of multiply low byte. s fast call/return mode select bit s = 0 : do not update into/from shadow registers s = 1 : certain registers loaded into/from shadow registers (fast mode) tblptr 21-bit table pointer (points to a program memory location). tablat 8-bit table latch. to time-out bit. tos top-of-stack. u unused or unchanged. wdt watchdog timer. wreg working register (accumulator). x dont care ( 0 or 1 ). the assembler will generate code with x = 0 . it is the recommended form of use for compatibility with all microchip software tools. z s 7-bit offset value for indirect addr essing of register files (source). z d 7-bit offset value for indirect addressing of register files (destination). { } optional argument. [text] indicates an indexed address. (text) the contents of text . [expr] specifies bit n of the register indicated by the pointer expr . ? assigned to. < > register bit field. ? in the set of. italics user defined term (font is courier). downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 267 pic18(l)f1xk22 figure 24-1: general format for instructions byte-oriented file register operations 15 10 9 8 7 0 d = 0 for result destination to be wreg register opcode d a f (file #) d = 1 for result destination to be file register (f) a = 0 to force access bank bit-oriented file register operations 15 12 11 9 8 7 0 opcode b (bit #) a f (file #) b = 3-bit position of bit in file register (f) literal operations 15 8 7 0 opcode k (literal) k = 8-bit immediate value byte to byte move operations (2-word) 15 12 11 0 opcode f (source file #) call , goto and branch operations 15 8 7 0 opcode n<7:0> (literal) n = 20-bit immediate value a = 1 for bsr to select bank f = 8-bit file register address a = 0 to force access bank a = 1 for bsr to select bank f = 8-bit file register address 15 12 11 0 1111 n<19:8> (literal) 15 12 11 0 1111 f (destination file #) f = 12-bit file register address control operations example instruction addwf myreg, w, b movff myreg1, myreg2 bsf myreg, bit, b movlw 7fh goto label 15 8 7 0 opcode n<7:0> (literal) 15 12 11 0 1111 n<19:8> (literal) call myfunc 15 11 10 0 opcode n<10:0> (literal) s = fast bit bra myfunc 15 8 7 0 opcode n<7:0> (literal) bc myfunc s downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 268 ? 2009-2016 microchip technology inc. table 24-2: pic18fxx xx instruction set mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb byte-oriented operations addwf addwfc andwf clrf comf cpfseq cpfsgt cpfslt decf decfsz dcfsnz incf incfsz infsnz iorwf movf movff movwf mulwf negf rlcf rlncf rrcf rrncf setf subfwb subwf subwfb swapf tstfsz xorwf f, d, a f, d, a f, d, a f, a f, d, a f, a f, a f, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f s , f d f, a f, a f, a f, d, a f, d, a f, d, a f, d, a f, a f, d, a f, d, a f, d, a f, d, a f, a f, d, a add wreg and f add wreg and carry bit to f and wreg with f clear f complement f compare f with wreg, skip = compare f with wreg, skip > compare f with wreg, skip < decrement f decrement f, skip if 0 decrement f, skip if not 0 increment f increment f, skip if 0 increment f, skip if not 0 inclusive or wreg with f move f move f s (source) to 1st word f d (destination) 2nd word move wreg to f multiply wreg with f negate f rotate left f through carry rotate left f (no carry) rotate right f through carry rotate right f (no carry) set f subtract f from wreg with borrow subtract wreg from f subtract wreg from f with borrow swap nibbles in f test f, skip if 0 exclusive or wreg with f 11 1 1 1 1 (2 or 3) 1 (2 or 3) 1 (2 or 3) 1 1 (2 or 3) 1 (2 or 3) 1 1 (2 or 3) 1 (2 or 3) 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 (2 or 3) 1 00100010 0001 0110 0001 0110 0110 0110 0000 0010 0100 0010 0011 0100 0001 0101 1100 1111 0110 0000 0110 0011 0100 0011 0100 0110 0101 0101 0101 0011 0110 0001 01da00da 01da 101a 11da 001a 010a 000a 01da 11da 11da 10da 11da 10da 00da 00da ffff ffff 111a 001a 110a 01da 01da 00da 00da 100a 01da 11da 10da 10da 011a 10da ffffffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffffffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff c, dc, z, ov, n c, dc, z, ov, n z, n z z, n none none none c, dc, z, ov, n none none c, dc, z, ov, n none none z, n z, n none none none c, dc, z, ov, n c, z, n z, n c, z, n z, n none c, dc, z, ov, n c, dc, z, ov, n c, dc, z, ov, n none none z, n 1, 2 1, 2 1, 2 2 1, 2 4 4 1, 2 1, 2, 3, 4 1, 2, 3, 4 1, 2 1, 2, 3, 4 4 1, 2 1, 2 1 1, 2 1, 2 1, 2 1, 2 4 1, 2 note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is 1 for a pin configured as i nput and is driven low by an external device, the data will be written back with a 0 . 2: if this instruction is executed on the tmr0 register (and where applicable, d = 1 ), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are two-word instructions. the se cond word of these instructions will be executed as a nop unless the first word of the instruction retrieves the information embe dded in these 16 bits. this ensur es that all program memory locations have a valid instruction. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 269 pic18(l)f1xk22 bit-oriented operations bcf bsf btfsc btfss btg f, b, a f, b, a f, b, a f, b, a f, b, a bit clear f bit set f bit test f, skip if clear bit test f, skip if set bit toggle f 11 1 (2 or 3) 1 (2 or 3) 1 10011000 1011 1010 0111 bbbabbba bbba bbba bbba ffffffff ffff ffff ffff ffff ffff ffff ffff ffff none none none none none 1, 2 1, 2 3, 4 3, 4 1, 2 control operations bc bn bnc bnn bnov bnz bov bra bz call clrwdt daw goto nop nop pop push rcall reset retfie retlw return sleep nn n n n n n n n k, s k n s k s branch if carry branch if negative branch if not carry branch if not negative branch if not overflow branch if not zero branch if overflow branch unconditionally branch if zero call subroutine 1st word 2nd word clear watchdog timer decimal adjust wreg go to address 1st word 2nd word no operation no operation pop top of return stack (tos) push top of return stack (tos) relative call software device reset return from interrupt enable return with literal in wreg return from subroutine go into standby mode 1 (2)1 (2) 1 (2) 1 (2) 1 (2) 1 (2) 1 (2) 2 1 (2) 2 1 1 2 1 1 1 1 2 1 2 2 2 1 11101110 1110 1110 1110 1110 1110 1101 1110 1110 1111 0000 0000 1110 1111 0000 1111 0000 0000 1101 0000 0000 0000 0000 0000 00100110 0011 0111 0101 0001 0100 0nnn 0000 110s kkkk 0000 0000 1111 kkkk 0000 xxxx 0000 0000 1nnn 0000 0000 1100 0000 0000 nnnnnnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn kkkk kkkk 0000 0000 kkkk kkkk 0000 xxxx 0000 0000 nnnn 1111 0001 kkkk 0001 0000 nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn kkkk kkkk 0100 0111 kkkk kkkk 0000 xxxx 0110 0101 nnnn 1111 000s kkkk 001s 0011 none none none none none none none none none none to , pd c none none none none none none all gie/gieh, peie/giel none none to , pd 4 table 24-2: pic18fxxxx inst ruction set (continued) mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is 1 for a pin configured as i nput and is driven low by an external device, the data will be written back with a 0 . 2: if this instruction is executed on the tmr0 register (and where applicable, d = 1 ), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are two-word instructions. the se cond word of these instructions will be executed as a nop unless the first word of the instruction retrieves the information embe dded in these 16 bits. this ensures that all program memory locations have a valid instruction. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 270 ? 2009-2016 microchip technology inc. literal operations addlw andlw iorlw lfsr movlb movlw mullw retlw sublw xorlw kk k f, k k k k k k k add literal and wreg and literal with wreg inclusive or literal with wreg move literal (12-bit) 2nd word to fsr(f) 1st word move literal to bsr<3:0> move literal to wreg multiply literal with wreg return with literal in wreg subtract wreg from literal exclusive or literal with wreg 11 1 2 1 1 1 2 1 1 00000000 0000 1110 1111 0000 0000 0000 0000 0000 0000 11111011 1001 1110 0000 0001 1110 1101 1100 1000 1010 kkkkkkkk kkkk 00ff kkkk 0000 kkkk kkkk kkkk kkkk kkkk kkkkkkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk c, dc, z, ov, n z, n z, n none none none none none c, dc, z, ov, n z, n data memory ? program memory operations tblrd* tblrd*+ tblrd*- tblrd+* tblwt* tblwt*+ tblwt*- tblwt+* table read table read with post-increment table read with post-decrement table read with pre-increment table write table write with post-increment table write with post-decrement table write with pre-increment 22 00000000 0000 0000 0000 0000 0000 0000 00000000 00000000 0000 0000 0000 0000 00000000 0000 0000 0000 0000 0000 0000 10001001 1010 1011 1100 1101 1110 1111 none none none none none none none none table 24-2: pic18fxxxx inst ruction set (continued) mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is 1 for a pin configured as i nput and is driven low by an external device, the data will be written back with a 0 . 2: if this instruction is executed on the tmr0 register (and where applicable, d = 1 ), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are two-word instructions. the se cond word of these instructions will be executed as a nop unless the first word of the instruction retrieves the information embe dded in these 16 bits. this ensur es that all program memory locations have a valid instruction. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 271 pic18(l)f1xk22 24.1.1 standard instruction set addlw add literal to w syntax: addlw k operands: 0 ? k ? 255 operation: (w) + k ? w status affected: n, ov, c, dc, z encoding: 0000 1111 kkkk kkkk description: the contents of w are added to the 8-bit literal k and the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal k process data write to w example : addlw 15h before instruction w = 10h after instruction w = 25h addwf add w to f syntax: addwf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (w) + (f) ? dest status affected: n, ov, c, dc, z encoding: 0010 01da ffff ffff description: add w to register f. if d is 0 , the result is stored in w. if d is 1 , the result is stored back in register f (default). if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example : addwf reg, 0, 0 before instruction w = 17h reg = 0c2h after instruction w = 0d9h reg = 0c2h note: all pic18 instructions may take an optional label argument preceding the instruction mnemonic for use in symbolic addressing. if a label is used, the instruction format then becomes: {label} instruction argument(s ). downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 272 ? 2009-2016 microchip technology inc. addwfc add w and carry bit to f syntax: addwfc f {,d {,a}} operands: 0 ? f ? 255 d ?? [ 0 , 1 ] a ?? [ 0 , 1 ] operation: (w) + (f) + (c) ? dest status affected: n,ov, c, dc, z encoding: 0010 00da ffff ffff description: add w, the carry flag and data mem- ory location f. if d is 0 , the result is placed in w. if d is 1 , the result is placed in data memory location f. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example : addwfc reg, 0, 1 before instruction carry bit = 1 reg = 02h w= 4 d h after instruction carry bit = 0 reg = 02h w = 50h andlw and literal with w syntax: andlw k operands: 0 ? k ? 255 operation: (w) .and. k ? w status affected: n, z encoding: 0000 1011 kkkk kkkk description: the contents of w are anded with the 8-bit literal k. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal k process data write to w example : andlw 05fh before instruction w=a 3 h after instruction w = 03h downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 273 pic18(l)f1xk22 andwf and w with f syntax: andwf f {,d {,a}} operands: 0 ? f ? 255 d ?? [ 0 , 1 ] a ?? [ 0 , 1 ] operation: (w) .and. (f) ? dest status affected: n, z encoding: 0001 01da ffff ffff description: the contents of w are anded with register f. if d is 0 , the result is stored in w. if d is 1 , the result is stored back in register f (default). if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example : andwf reg, 0, 0 before instruction w = 17h reg = c2h after instruction w = 02h reg = c2h bc branch if carry syntax: bc n operands: -128 ? n ? 127 operation: if carry bit is 1 (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0010 nnnn nnnn description: if the carry bit is 1 , then the program will branch. the 2s complement number 2n is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a 2-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal n process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal n process data no operation example : here bc 5 before instruction pc = address (here) after instruction if carry = 1 ; pc = address (here + 12) if carry = 0 ; pc = address (here + 2) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 274 ? 2009-2016 microchip technology inc. bcf bit clear f syntax: bcf f, b {,a} operands: 0 ? f ? 255 0 ? b ? 7 a ?? [ 0 , 1 ] operation: 0 ? f status affected: none encoding: 1001 bbba ffff ffff description: bit b in register f is cleared. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write register f example : bcf flag_reg, 7, 0 before instruction flag_reg = c7h after instruction flag_reg = 47h bn branch if negative syntax: bn n operands: -128 ? n ? 127 operation: if negative bit is 1 (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0110 nnnn nnnn description: if the negative bit is 1 , then the program will branch. the 2s complement number 2n is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a 2-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal n process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal n process data no operation example : here bn jump before instruction pc = address (here) after instruction if negative = 1 ; pc = address (jump) if negative = 0 ; pc = address (here + 2) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 275 pic18(l)f1xk22 bnc branch if not carry syntax: bnc n operands: -128 ? n ? 127 operation: if carry bit is 0 (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0011 nnnn nnnn description: if the carry bit is 0 , then the program will branch. the 2s complement number 2n is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a 2-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal n process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal n process data no operation example : here bnc jump before instruction pc = address (here) after instruction if carry = 0 ; pc = address (jump) if carry = 1 ; pc = address (here + 2) bnn branch if not negative syntax: bnn n operands: -128 ? n ? 127 operation: if negative bit is 0 (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0111 nnnn nnnn description: if the negative bit is 0 , then the program will branch. the 2s complement number 2n is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a 2-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal n process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal n process data no operation example : here bnn jump before instruction pc = address (here) after instruction if negative = 0 ; pc = address (jump) if negative = 1 ; pc = address (here + 2) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 276 ? 2009-2016 microchip technology inc. bnov branch if not overflow syntax: bnov n operands: -128 ? n ? 127 operation: if overflow bit is 0 (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0101 nnnn nnnn description: if the overflow bit is 0 , then the program will branch. the 2s complement number 2n is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a 2-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal n process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal n process data no operation example : here bnov jump before instruction pc = address (here) after instruction if overflow = 0 ; pc = address (jump) if overflow = 1 ; pc = address (here + 2) bnz branch if not zero syntax: bnz n operands: -128 ? n ? 127 operation: if zero bit is 0 (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0001 nnnn nnnn description: if the zero bit is 0 , then the program will branch. the 2s complement number 2n is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a 2-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal n process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal n process data no operation example : here bnz jump before instruction pc = address (here) after instruction if zero = 0 ; pc = address (jump) if zero = 1 ; pc = address (here + 2) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 277 pic18(l)f1xk22 bra unconditional branch syntax: bra n operands: -1024 ? n ? 1023 operation: (pc) + 2 + 2n ? pc status affected: none encoding: 1101 0nnn nnnn nnnn description: add the 2s complement number 2n to the pc. since the pc will have incre- mented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is a 2-cycle instruction. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal n process data write to pc no operation no operation no operation no operation example : here bra jump before instruction pc = address (here) after instruction pc = address (jump) bsf bit set f syntax: bsf f, b {,a} operands: 0 ? f ? 255 0 ? b ? 7 a ?? [ 0 , 1 ] operation: 1 ? f status affected: none encoding: 1000 bbba ffff ffff description: bit b in register f is set. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write register f example : bsf flag_reg, 7, 1 before instruction flag_reg = 0ah after instruction flag_reg = 8ah downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 278 ? 2009-2016 microchip technology inc. btfsc bit test file, skip if clear syntax: btfsc f, b {,a} operands: 0 ? f ? 255 0 ? b ? 7 a ?? [ 0 , 1 ] operation: skip if (f) = 0 status affected: none encoding: 1011 bbba ffff ffff description: if bit b in register f is 0 , then the next instruction is skipped. if bit b is 0 , then the next instruction fetched during the current instruction execution is discarded and a nop is executed instead, making this a 2-cycle instruction. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register f process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here false true btfsc: : flag, 1, 0 before instruction pc = address (here) after instruction if flag<1> = 0 ; pc = address (true) if flag<1> = 1 ; pc = address (false) btfss bit test file, skip if set syntax: btfss f, b {,a} operands: 0 ? f ? 255 0 ? b < 7 a ?? [ 0 , 1 ] operation: skip if (f) = 1 status affected: none encoding: 1010 bbba ffff ffff description: if bit b in register f is 1 , then the next instruction is skipped. if bit b is 1 , then the next instruction fetched during the current instruction execution is discarded and a nop is executed instead, making this a 2-cycle instruction. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register f process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : herefalse true btfss: : flag, 1, 0 before instruction pc = address (here) after instruction if flag<1> = 0 ; pc = address (false) if flag<1> = 1 ; pc = address (true) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 279 pic18(l)f1xk22 btg bit toggle f syntax: btg f, b {,a} operands: 0 ? f ? 255 0 ? b < 7 a ?? [ 0 , 1 ] operation: (f ) ? f status affected: none encoding: 0111 bbba ffff ffff description: bit b in data memory location f is inverted. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write register f example : btg portc, 4, 0 before instruction: portc = 0111 0101 [75h] after instruction: portc = 0110 0101 [65h] bov branch if overflow syntax: bov n operands: -128 ? n ? 127 operation: if overflow bit is 1 (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0100 nnnn nnnn description: if the overflow bit is 1 , then the program will branch. the 2s complement number 2n is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a 2-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal n process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal n process data no operation example : here bov jump before instruction pc = address (here) after instruction if overflow = 1 ; pc = address (jump) if overflow = 0 ; pc = address (here + 2) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 280 ? 2009-2016 microchip technology inc. bz branch if zero syntax: bz n operands: -128 ? n ? 127 operation: if zero bit is 1 (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0000 nnnn nnnn description: if the zero bit is 1 , then the program will branch. the 2s complement number 2n is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a 2-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal n process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal n process data no operation example : here bz jump before instruction pc = address (here) after instruction if zero = 1 ; pc = address (jump) if zero = 0 ; pc = address (here + 2) call subroutine call syntax: call k {,s} operands: 0 ? k ? 1048575 s ?? [ 0 , 1 ] operation: (pc) + 4 ? tos, k ? pc<20:1>, if s = 1 (w) ? ws, (status) ? statuss, (bsr) ? bsrs status affected: none encoding: 1st word (k<7:0>) 2nd word(k<19:8>) 11101111 110s k 19 kkk k 7 kkk kkkk kkkk 0 kkkk 8 description: subroutine call of entire 2-mbyte memory range. first, return address (pc + 4) is pushed onto the return stack. if s = 1 , the w, status and bsr registers are also pushed into their respective shadow registers, ws, statuss and bsrs. if s = 0 , no update occurs (default). then, the 20-bit value k is loaded into pc<20:1>. call is a 2-cycle instruction. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal k<7:0>, push pc to stack read literal k<19:8>, write to pc no operation no operation no operation no operation example : here call there, 1 before instruction pc = address (here) after instruction pc = address (there) tos = address (here + 4) ws = w bsrs = bsr statuss = status downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 281 pic18(l)f1xk22 clrf clear f syntax: clrf f {,a} operands: 0 ? f ? 255 a ?? [ 0 , 1 ] operation: 000h ? f 1 ? z status affected: z encoding: 0110 101a ffff ffff description: clears the contents of the specified register. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write register f example : clrf flag_reg, 1 before instruction flag_reg = 5ah after instruction flag_reg = 00h clrwdt clear watchdog timer syntax: clrwdt operands: none operation: 000h ? wdt, 000h ? wdt postscaler, 1 ? to, 1 ? pd status affected: to , pd encoding: 0000 0000 0000 0100 description: clrwdt instruction resets the watchdog timer. it also resets the post- scaler of the wdt. status bits, to and pd , are set. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation process data no operation example : clrwdt before instruction wdt counter = ? after instruction wdt counter = 00h wdt postscaler = 0 to = 1 pd = 1 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 282 ? 2009-2016 microchip technology inc. comf complement f syntax: comf f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (f ) ? dest status affected: n, z encoding: 0001 11da ffff ffff description: the contents of register f are complemented. if d is 0 , the result is stored in w. if d is 1 , the result is stored back in register f (default). if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example : comf reg, 0, 0 before instruction reg = 13h after instruction reg = 13h w=e c h cpfseq compare f with w, skip if f = w syntax: cpfseq f {,a} operands: 0 ? f ? 255 a ? [ 0 , 1 ] operation: (f) C (w), skip if (f) = (w) (unsigned comparison) status affected: none encoding: 0110 001a ffff ffff description: compares the contents of data memory location f to the contents of w by performing an unsigned subtraction. if f = w, then the fetched instruction is discarded and a nop is executed instead, making this a 2-cycle instruction. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register f process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfseq reg, 0 nequal : equal : before instruction pc address = here w= ? reg = ? after instruction if reg = w; pc = address (equal) if reg ? w; pc = address (nequal) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 283 pic18(l)f1xk22 cpfsgt compare f with w, skip if f > w syntax: cpfsgt f {,a} operands: 0 ? f ? 255 a ? [ 0 , 1 ] operation: (f) C ?? w), skip if (f) > (w) (unsigned comparison) status affected: none encoding: 0110 010a ffff ffff description: compares the contents of data memory location f to the contents of the w by performing an unsigned subtraction. if the contents of f are greater than the contents of wreg , then the fetched instruction is discarded and a nop is executed instead, making this a 2-cycle instruction. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register f process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfsgt reg, 0 ngreater : greater : before instruction pc = address (here) w= ? after instruction if reg ? w; pc = address (greater) if reg ? w; pc = address (ngreater) cpfslt compare f with w, skip if f < w syntax: cpfslt f {,a} operands: 0 ? f ? 255 a ? [ 0 , 1 ] operation: (f) C ?? w), skip if (f) < (w) (unsigned comparison) status affected: none encoding: 0110 000a ffff ffff description: compares the contents of data memory location f to the contents of w by performing an unsigned subtraction. if the contents of f are less than the contents of w, then the fetched instruction is discarded and a nop is executed instead, making this a 2-cycle instruction. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). words: 1 cycles: 1(2) note: three cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register f process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfslt reg, 1 nless : less : before instruction pc = address (here) w= ? after instruction if reg < w; pc = address (less) if reg ? w; pc = address (nless) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 284 ? 2009-2016 microchip technology inc. daw decimal adjust w register syntax: daw operands: none operation: if [w<3:0> > 9] or [dc = 1] then (w<3:0>) + 6 ? w<3:0>; else ( w<3:0>) ? w<3:0>; if [w<7:4> + dc > 9] or [c = 1] then ( w<7:4>) + 6 + dc ? w<7:4>; else (w<7:4>) + dc ? w<7:4> status affected: c encoding: 0000 0000 0000 0111 description: daw adjusts the 8-bit value in w, result- ing from the earlier addition of two vari- ables (each in packed bcd format) and produces a correct packed bcd result. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register w process data write w example1 : daw before instruction w=a 5 h c= 0 dc = 0 after instruction w = 05h c= 1 dc = 0 example 2 : before instruction w=c e h c= 0 dc = 0 after instruction w = 34h c= 1 dc = 0 decf decrement f syntax: decf f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (f) C 1 ? dest status affected: c, dc, n, ov, z encoding: 0000 01da ffff ffff description: decrement register f. if d is 0 , the result is stored in w. if d is 1 , the result is stored back in register f (default). if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example : decf cnt, 1, 0 before instruction cnt = 01h z= 0 after instruction cnt = 00h z= 1 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 285 pic18(l)f1xk22 decfsz decrement f, skip if 0 syntax: decfsz f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) C 1 ? dest, skip if result = 0 status affected: none encoding: 0010 11da ffff ffff description: the contents of register f are decremented. if d is 0 , the result is placed in w. if d is 1 , the result is placed back in register f (default). if the result is 0 , the next instruction, which is already fetched, is discarded and a nop is executed instead, making it a 2-cycle instruction. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here decfsz cnt, 1, 1 goto loop continue before instruction pc = address (here) after instruction cnt = cnt - 1 if cnt = 0 ; pc = address (continue) if cnt ? 0 ; pc = address (here + 2) dcfsnz decrement f, skip if not 0 syntax: dcfsnz f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (f) C 1 ? dest, skip if result ? 0 status affected: none encoding: 0100 11da ffff ffff description: the contents of register f are decremented. if d is 0 , the result is placed in w. if d is 1 , the result is placed back in register f (default). if the result is not 0 , the next instruction, which is already fetched, is discarded and a nop is executed instead, making it a 2-cycle instruction. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here dcfsnz temp, 1, 0 zero : nzero : before instruction temp = ? after instruction temp = temp C 1, if temp = 0 ; pc = address (zero) if temp ? 0 ; pc = address (nzero) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 286 ? 2009-2016 microchip technology inc. goto unconditional branch syntax: goto k operands: 0 ? k ? 1048575 operation: k ? pc<20:1> status affected: none encoding: 1st word (k<7:0>) 2nd word(k<19:8>) 11101111 1111 k 19 kkk k 7 kkk kkkk kkkk 0 kkkk 8 description: goto allows an unconditional branch anywhere within entire 2-mbyte memory range. the 20-bit value k is loaded into pc<20:1>. goto is always a 2-cycle instruction. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal k<7:0>, no operation read literal k<19:8>, write to pc no operation no operation no operation no operation example : goto there after instruction pc = address (there) incf increment f syntax: incf f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (f) + 1 ? dest status affected: c, dc, n, ov, z encoding: 0010 10da ffff ffff description: the contents of register f are incremented. if d is 0 , the result is placed in w. if d is 1 , the result is placed back in register f (default). if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example : incf cnt, 1, 0 before instruction cnt = ffh z= 0 c= ? dc = ? after instruction cnt = 00h z= 1 c= 1 dc = 1 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 287 pic18(l)f1xk22 incfsz increment f, skip if 0 syntax: incfsz f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (f) + 1 ? dest, skip if result = 0 status affected: none encoding: 0011 11da ffff ffff description: the contents of register f are incremented. if d is 0 , the result is placed in w. if d is 1 , the result is placed back in register f (default). if the result is 0 , the next instruction, which is already fetched, is discarded and a nop is executed instead, making it a 2-cycle instruction. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here incfsz cnt, 1, 0 nzero : zero : before instruction pc = address (here) after instruction cnt = cnt + 1 if cnt = 0 ; pc = address (zero) if cnt ? 0 ; pc = address (nzero) infsnz increment f, skip if not 0 syntax: infsnz f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (f) + 1 ? dest, skip if result ? 0 status affected: none encoding: 0100 10da ffff ffff description: the contents of register f are incremented. if d is 0 , the result is placed in w. if d is 1 , the result is placed back in register f (default). if the result is not 0 , the next instruction, which is already fetched, is discarded and a nop is executed instead, making it a 2-cycle instruction. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here infsnz reg, 1, 0 zero nzero before instruction pc = address (here) after instruction reg = reg + 1 if reg ? 0 ; pc = address (nzero) if reg = 0 ; pc = address (zero) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 288 ? 2009-2016 microchip technology inc. iorlw inclusive or literal with w syntax: iorlw k operands: 0 ? k ? 255 operation: (w) .or. k ? w status affected: n, z encoding: 0000 1001 kkkk kkkk description: the contents of w are ored with the 8- bit literal k. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal k process data write to w example : iorlw 35h before instruction w=9 a h after instruction w=b f h iorwf inclusive or w with f syntax: iorwf f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (w) .or. (f) ? dest status affected: n, z encoding: 0001 00da ffff ffff description: inclusive or w with register f. if d is 0 , the result is placed in w. if d is 1 , the result is placed back in register f (default). if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example : iorwf result, 0, 1 before instruction result = 13h w = 91h after instruction result = 13h w = 93h downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 289 pic18(l)f1xk22 lfsr load fsr syntax: lfsr f, k operands: 0 ? f ? 2 0 ? k ? 4095 operation: k ? fsrf status affected: none encoding: 11101111 11100000 00ff k 7 kkk k 11 kkk kkkk description: the 12-bit literal k is loaded into the file select register pointed to by f. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal k msb process data write literal k msb to fsrfh decode read literal k lsb process data write literal k to fsrfl example : lfsr 2, 3abh after instruction fsr2h = 03h fsr2l = abh movf move f syntax: movf f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: f ? dest status affected: n, z encoding: 0101 00da ffff ffff description: the contents of register f are moved to a destination dependent upon the status of d. if d is 0 , the result is placed in w. if d is 1 , the result is placed back in register f (default). location f can be anywhere in the 256-byte bank. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write w example : movf reg, 0, 0 before instruction reg = 22h w= f f h after instruction reg = 22h w = 22h downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 290 ? 2009-2016 microchip technology inc. movff move f to f syntax: movff f s ,f d operands: 0 ? f s ? 4095 0 ? f d ? 4095 operation: (f s ) ? f d status affected: none encoding: 1st word (source) 2nd word (destin.) 11001111 ffffffff ffffffff fff f s fff f d description: the contents of source register f s are moved to destination register f d . location of source f s can be anywhere in the 4096-byte data space (000h to fffh) and location of destination f d can also be anywhere from 000h to fffh. either source or destination can be w (a useful special situation). movff is particularly useful for transferring a data memory location to a peripheral register (such as the transmit buffer or an i/o port). the movff instruction cannot use the pcl, tosu, tosh or tosl as the destination register. words: 2 cycles: 2 (3) q cycle activity: q1 q2 q3 q4 decode read register f (src) process data no operation decode no operation no dummy read no operation write register f (dest) example : movff reg1, reg2 before instruction reg1 = 33h reg2 = 11h after instruction reg1 = 33h reg2 = 33h movlb move literal to low nibble in bsr syntax: movlb k operands: 0 ? k ? 255 operation: k ? bsr status affected: none encoding: 0000 0001 0000 kkkk description: the 8-bit literal k is loaded into the bank select register (bsr). the value of bsr<7:4> always remains 0 , regardless of the value of k 7 :k 4 . words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal k process data write literal k to bsr example : movlb 5 before instruction bsr register = 02h after instruction bsr register = 05h downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 291 pic18(l)f1xk22 movlw move literal to w syntax: movlw k operands: 0 ? k ? 255 operation: k ? w status affected: none encoding: 0000 1110 kkkk kkkk description: the 8-bit literal k is loaded into w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal k process data write to w example : movlw 5ah after instruction w=5 a h movwf move w to f syntax: movwf f {,a} operands: 0 ? f ? 255 a ? [ 0 , 1 ] operation: (w) ? f status affected: none encoding: 0110 111a ffff ffff description: move data from w to register f. location f can be anywhere in the 256-byte bank. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write register f example : movwf reg, 0 before instruction w=4 f h reg = ffh after instruction w=4 f h reg = 4fh downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 292 ? 2009-2016 microchip technology inc. mullw multiply literal with w syntax: mullw k operands: 0 ? k ? 255 operation: (w) x k ? prodh:prodl status affected: none encoding: 0000 1101 kkkk kkkk description: an unsigned multiplication is carried out between the contents of w and the 8-bit literal k. the 16-bit result is placed in the prodh:prodl register pair. prodh contains the high byte. w is unchanged. none of the status flags are affected. note that neither overflow nor carry is possible in this operation. a zero result is possible but not detected. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal k process data write registers prodh: prodl example : mullw 0c4h before instruction w= e 2 h prodh = ? prodl = ? after instruction w= e 2 h prodh = adh prodl = 08h mulwf multiply w with f syntax: mulwf f {,a} operands: 0 ? f ? 255 a ? [ 0 , 1 ] operation: (w) x (f) ? prodh:prodl status affected: none encoding: 0000 001a ffff ffff description: an unsigned multiplication is carried out between the contents of w and the register file location f. the 16-bit result is stored in the prodh:prodl register pair. prodh contains the high byte. both w and f are unchanged. none of the status flags are affected. note that neither overflow nor carry is possible in this operation. a zero result is possible but not detected. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write registers prodh: prodl example : mulwf reg, 1 before instruction w= c 4 h reg = b5h prodh = ? prodl = ? after instruction w= c 4 h reg = b5h prodh = 8ah prodl = 94h downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 293 pic18(l)f1xk22 negf negate f syntax: negf f {,a} operands: 0 ? f ? 255 a ? [ 0 , 1 ] operation: ( f ) + 1 ? f status affected: n, ov, c, dc, z encoding: 0110 110a ffff ffff description: location f is negated using twos complement. the result is placed in the data memory location f. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write register f example : negf reg, 1 before instruction reg = 0011 1010 [3ah] after instruction reg = 1100 0110 [c6h] nop no operation syntax: nop operands: none operation: no operation status affected: none encoding: 00001111 0000xxxx 0000xxxx 0000xxxx description: no operation. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation example : none. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 294 ? 2009-2016 microchip technology inc. pop pop top of return stack syntax: pop operands: none operation: (tos) ? bit bucket status affected: none encoding: 0000 0000 0000 0110 description: the tos value is pulled off the return stack and is discarded. the tos value then becomes the previous value that was pushed onto the return stack. this instruction is provided to enable the user to properly manage the return stack to incorporate a software stack. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation pop tos value no operation example : pop goto new before instruction tos = 0031a2h stack (1 level down) = 014332h after instruction tos = 014332h pc = new push push top of return stack syntax: push operands: none operation: (pc + 2) ? tos status affected: none encoding: 0000 0000 0000 0101 description: the pc + 2 is pushed onto the top of the return stack. the previous tos value is pushed down on the stack. this instruction allows implementing a software stack by modifying tos and then pushing it onto the return stack. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode push pc + 2 onto return stack no operation no operation example : push before instruction tos = 345ah pc = 0124h after instruction pc = 0126h tos = 0126h stack (1 level down) = 345ah downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 295 pic18(l)f1xk22 rcall relative call syntax: rcall n operands: -1024 ? n ? 1023 operation: (pc) + 2 ? tos, (pc) + 2 + 2n ? pc status affected: none encoding: 1101 1nnn nnnn nnnn description: subroutine call with a jump up to 1k from the current location. first, return address (pc + 2) is pushed onto the stack. then, add the 2s complement number 2n to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is a 2-cycle instruction. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal n push pc to stack process data write to pc no operation no operation no operation no operation example : here rcall jump before instruction pc = address (here) after instruction pc = address (jump) tos = address (here + 2) reset reset syntax: reset operands: none operation: reset all registers and flags that are affected by a mclr reset. status affected: all encoding: 0000 0000 1111 1111 description: this instruction provides a way to execute a mclr reset by software. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode start reset no operation no operation example : reset after instruction registers = reset value flags* = reset value downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 296 ? 2009-2016 microchip technology inc. retfie return from interrupt syntax: retfie {s} operands: s ? [ 0 , 1 ] operation: (tos) ? pc, 1 ? gie/gieh or peie/giel, if s = 1 (ws) ? w, (statuss) ? status, (bsrs) ? bsr, pclatu, pclath are unchanged. status affected: gie/gieh, peie/giel encoding: 0000 0000 0001 000s description: return from interrupt. stack is popped and top-of-stack (tos) is loaded into the pc. interrupts are enabled by setting either the high or low priority global interrupt enable bit. if s = 1 , the contents of the shadow registers, ws, statuss and bsrs, are loaded into their corresponding registers, w, status and bsr. if s = 0 , no update of these registers occurs (default). words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation pop pc from stack set gieh or giel no operation no operation no operation no operation example : retfie 1 after interrupt pc = tos w= w s bsr = bsrs status = statuss gie/gieh, peie/giel = 1 retlw return literal to w syntax: retlw k operands: 0 ? k ? 255 operation: k ? w, (tos) ? pc, pclatu, pclath are unchanged status affected: none encoding: 0000 1100 kkkk kkkk description: w is loaded with the 8-bit literal k. the program counter is loaded from the top of the stack (the return address). the high address latch (pclath) remains unchanged. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal k process data pop pc from stack, write to w no operation no operation no operation no operation example : call table ; w contains table ; offset value ; w now has ; table value : table addwf pcl ; w = offset retlw k0 ; begin table retlw k1 ; : : retlw kn ; end of table before instruction w = 07h after instruction w = value of kn downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 297 pic18(l)f1xk22 return return from subroutine syntax: return {s} operands: s ? [ 0 , 1 ] operation: (tos) ? pc, if s = 1 (ws) ? w, (statuss) ? status, (bsrs) ? bsr, pclatu, pclath are unchanged status affected: none encoding: 0000 0000 0001 001s description: return from subroutine. the stack is popped and the top of the stack (tos) is loaded into the program counter. if s= 1 , the contents of the shadow registers, ws, statuss and bsrs, are loaded into their corresponding registers, w, status and bsr. if s = 0 , no update of these registers occurs (default). words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation process data pop pc from stack no operation no operation no operation no operation example : return after instruction: pc = tos rlcf rotate left f through carry syntax: rlcf f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (f) ? dest, (f<7>) ? c, (c) ? dest<0> status affected: c, n, z encoding: 0011 01da ffff ffff description: the contents of register f are rotated one bit to the left through the carry flag. if d is 0 , the result is placed in w. if d is 1 , the result is stored back in register f (default). if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example : rlcf reg, 0, 0 before instruction reg = 1110 0110 c= 0 after instruction reg = 1110 0110 w = 1100 1100 c= 1 c register f downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 298 ? 2009-2016 microchip technology inc. rlncf rotate left f (no carry) syntax: rlncf f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (f) ? dest, (f<7>) ? dest<0> status affected: n, z encoding: 0100 01da ffff ffff description: the contents of register f are rotated one bit to the left. if d is 0 , the result is placed in w. if d is 1 , the result is stored back in register f (default). if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example : rlncf reg, 1, 0 before instruction reg = 1010 1011 after instruction reg = 0101 0111 register f rrcf rotate right f through carry syntax: rrcf f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (f) ? dest, (f<0>) ? c, (c) ? dest<7> status affected: c, n, z encoding: 0011 00da ffff ffff description: the contents of register f are rotated one bit to the right through the carry flag. if d is 0 , the result is placed in w. if d is 1 , the result is placed back in register f (default). if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example : rrcf reg, 0, 0 before instruction reg = 1110 0110 c= 0 after instruction reg = 1110 0110 w = 0111 0011 c= 0 c register f downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 299 pic18(l)f1xk22 rrncf rotate right f (no carry) syntax: rrncf f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (f) ? dest, (f<0>) ? dest<7> status affected: n, z encoding: 0100 00da ffff ffff description: the contents of register f are rotated one bit to the right. if d is 0 , the result is placed in w. if d is 1 , the result is placed back in register f (default). if a is 0 , the access bank will be selected, overriding the bsr value. if a is 1 , then the bank will be selected as per the bsr value (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example 1 : rrncf reg, 1, 0 before instruction reg = 1101 0111 after instruction reg = 1110 1011 example 2 : rrncf reg, 0, 0 before instruction w=? reg = 1101 0111 after instruction w = 1110 1011 reg = 1101 0111 register f setf set f syntax: setf f {,a} operands: 0 ? f ? 255 a ?? [ 0 , 1 ] operation: ffh ? f status affected: none encoding: 0110 100a ffff ffff description: the contents of the specified register are set to ffh. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write register f example : setf reg, 1 before instruction reg = 5ah after instruction reg = ffh downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 300 ? 2009-2016 microchip technology inc. sleep enter sleep mode syntax: sleep operands: none operation: 00h ? wdt, 0 ? wdt postscaler, 1 ? to , 0 ? pd status affected: to , pd encoding: 0000 0000 0000 0011 description: the power-down status bit (pd ) is cleared. the time-out status bit (to ) is set. watchdog timer and its posts- caler are cleared. the processor is put into sleep mode with the oscillator stopped. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation process data go to sleep example : sleep before instruction to =? pd =? after instruction to = 1 ? pd = 0 ? if wdt causes wake-up, this bit is cleared. subfwb subtract f from w with borrow syntax: subfwb f {,d {,a}} operands: 0 ?? f ?? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (w) C (f) C (c ) ?? dest status affected: n, ov, c, dc, z encoding: 0101 01da ffff ffff description: subtract register f and carry flag (borrow) from w (2s complement method). if d is 0 , the result is stored in w. if d is 1 , the result is stored in register f (default). if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in index ed litera l offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example 1 : subfwb reg, 1, 0 before instruction reg = 3 w=2 c= 1 after instruction reg = ff w=2 c= 0 z= 0 n= 1 ; result is negative example 2 : subfwb reg, 0, 0 before instruction reg = 2 w=5 c= 1 after instruction reg = 2 w=3 c= 1 z= 0 n= 0 ; result is positive example 3 : subfwb reg, 1, 0 before instruction reg = 1 w=2 c= 0 after instruction reg = 0 w=2 c= 1 z= 1 ; result is zero n= 0 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 301 pic18(l)f1xk22 sublw subtract w from literal syntax: sublw k operands: 0 ?? k ?? 255 operation: k C (w) ?? w status affected: n, ov, c, dc, z encoding: 0000 1000 kkkk kkkk description w is subtracted from the 8-bit literal k. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal k process data write to w example 1: sublw 02h before instruction w = 01h c= ? after instruction w = 01h c= 1 ; result is positive z= 0 n= 0 example 2 : sublw 02h before instruction w = 02h c= ? after instruction w = 00h c= 1 ; result is zero z= 1 n= 0 example 3 : sublw 02h before instruction w = 03h c= ? after instruction w = ffh ; (2s complement) c= 0 ; result is negative z= 0 n= 1 subwf subtract w from f syntax: subwf f {,d {,a}} operands: 0 ?? f ?? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (f) C (w) ?? dest status affected: n, ov, c, dc, z encoding: 0101 11da ffff ffff description: subtract w from register f (2s complement method). if d is 0 , the result is stored in w. if d is 1 , the result is stored back in register f (default). if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in index ed litera l offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example 1 : subwf reg, 1, 0 before instruction reg = 3 w=2 c= ? after instruction reg = 1 w=2 c= 1 ; result is positive z= 0 n= 0 example 2 : subwf reg, 0, 0 before instruction reg = 2 w=2 c= ? after instruction reg = 2 w= 0 c= 1 ; result is zero z= 1 n= 0 example 3 : subwf reg, 1, 0 before instruction reg = 1 w=2 c= ? after instruction reg = ffh ;(2s complement) w=2 c= 0 ; result is negative z= 0 n= 1 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 302 ? 2009-2016 microchip technology inc. subwfb subtract w from f with borrow syntax: subwfb f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (f) C (w) C (c ) ?? dest status affected: n, ov, c, dc, z encoding: 0101 10da ffff ffff description: subtract w and the carry flag (borrow) from register f (2s comple- ment method). if d is 0 , the result is stored in w. if d is 1 , the result is stored back in register f (default). if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example 1 : subwfb reg, 1, 0 before instruction reg = 19h (0001 1001) w =0dh (0000 1101) c= 1 after instruction reg = 0ch (0000 1011) w =0dh (0000 1101) c= 1 z= 0 n= 0 ; result is positive example 2 : subwfb reg, 0, 0 before instruction reg = 1bh (0001 1011) w =1ah (0001 1010) c= 0 after instruction reg = 1bh (0001 1011) w = 00h c= 1 z= 1 ; result is zero n= 0 example 3: subwfb reg, 1, 0 before instruction reg = 03h (0000 0011) w =0eh (0000 1101) c= 1 after instruction reg = f5h (1111 0100) ; [2s comp] w =0eh (0000 1101) c= 0 z= 0 n= 1 ; result is negative swapf swap f syntax: swapf f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (f<3:0>) ? dest<7:4>, (f<7:4>) ? dest<3:0> status affected: none encoding: 0011 10da ffff ffff description: the upper and lower nibbles of register f are exchanged. if d is 0 , the result is placed in w. if d is 1 , the result is placed in register f (default). if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example : swapf reg, 1, 0 before instruction reg = 53h after instruction reg = 35h downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 303 pic18(l)f1xk22 tblrd table read syntax: tblrd ( *; *+; *-; +*) operands: none operation: if tblrd *, (prog mem (tblptr)) ? tablat; tblptr C no change; if tblrd *+, (prog mem (tblptr)) ? tablat; (tblptr) + 1 ? tblptr; if tblrd *-, (prog mem (tblptr)) ? tablat; (tblptr) C 1 ? tblptr; if tblrd +*, (tblptr) + 1 ? tblptr; (prog mem (tblptr)) ? tablat; status affected: none encoding: 0000 0000 0000 10nn nn=0 * =1 *+ =2 *- =3 +* description: this instruction is used to read the contents of program memory (p.m.). to address the program memory, a pointer called table pointer (tblptr) is used. the tblptr (a 21-bit pointer) points to each byte in the program memory. tblptr has a 2-mbyte address range. tblptr[0] = 0 : least significant byte of program memory word tblptr[0] = 1 : most significant byte of program memory word the tblrd instruction can modify the value of tblptr as follows: no change post-increment post-decrement pre-increment words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation no operation no operation (read program memory) no operation no operation (write tablat) tblrd table read (continued) example1 : tblrd *+ ; before instruction tablat = 55h tblptr = 00a356h memory (00a356h) = 34h after instruction tablat = 34h tblptr = 00a357h example2 : tblrd +* ; before instruction tablat = aah tblptr = 01a357h memory (01a357h) = 12h memory (01a358h) = 34h after instruction tablat = 34h tblptr = 01a358h downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 304 ? 2009-2016 microchip technology inc. tblwt table write syntax: tblwt ( *; *+; *-; +*) operands: none operation: if tblwt*, (tablat) ? holding register; tblptr C no change; if tblwt*+, (tablat) ? holding register; (tblptr) + 1 ? tblptr; if tblwt*-, (tablat) ? holding register; (tblptr) C 1 ? tblptr; if tblwt+*, (tblptr) + 1 ? tblptr; (tablat) ? holding register; status affected: none encoding: 0000 0000 0000 11nn nn=0 * =1 *+ =2 *- =3 +* description: this instruction uses the 3 lsbs of tblptr to determine which of the eight holding registers the tablat is writ- ten to. the holding registers are used to program the contents of program memory (p.m.). (refer to section 4.0 ?flash program memory? for additional details on programming flash memory.) the tblptr (a 21-bit pointer) points to each byte in the program memory. tblptr has a 2-mbyte address range. the lsb of the tblptr selects which byte of the program memory location to access. tblptr[0] = 0 : least significant byte of program memory word tblptr[0] = 1 : most significant byte of program memory word the tblwt instruction can modify the value of tblptr as follows: no change post-increment post-decrement pre-increment words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation no operation no operation (read tablat) no operation no operation (write to holding register ) tblwt table write (continued) example1 : tblwt *+; before instruction tablat = 55h tblptr = 00a356h holding register (00a356h) = ffh after instructions (table write completion) tablat = 55h tblptr = 00a357h holding register (00a356h) = 55h example 2 : tblwt +*; before instruction tablat = 34h tblptr = 01389ah holding register (01389ah) = ffh holding register (01389bh) = ffh after instruction (table write completion) tablat = 34h tblptr = 01389bh holding register (01389ah) = ffh holding register (01389bh) = 34h downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 305 pic18(l)f1xk22 tstfsz test f, skip if 0 syntax: tstfsz f {,a} operands: 0 ? f ? 255 a ? [ 0 , 1 ] operation: skip if f = 0 status affected: none encoding: 0110 011a ffff ffff description: if f = 0 , the next instruction fetched during the current instruction execution is discarded and a nop is executed, making this a 2-cycle instruction. if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register f process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here tstfsz cnt, 1 nzero : zero : before instruction pc = address (here) after instruction if cnt = 00h, pc = address (zero) if cnt ? 00h, pc = address (nzero) xorlw exclusive or literal with w syntax: xorlw k operands: 0 ?? k ?? 255 operation: (w) .xor. k ?? w status affected: n, z encoding: 0000 1010 kkkk kkkk description: the contents of w are xored with the 8-bit literal k. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal k process data write to w example : xorlw 0afh before instruction w=b 5 h after instruction w=1 a h downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 306 ? 2009-2016 microchip technology inc. xorwf exclusive or w with f syntax: xorwf f {,d {,a}} operands: 0 ? f ? 255 d ? [ 0 , 1 ] a ? [ 0 , 1 ] operation: (w) .xor. (f) ?? dest status affected: n, z encoding: 0001 10da ffff ffff description: exclusive or the contents of w with register f. if d is 0 , the result is stored in w. if d is 1 , the result is stored back in the register f (default). if a is 0 , the access bank is selected. if a is 1 , the bsr is used to select the gpr bank (default). if a is 0 and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 24.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example : xorwf reg, 1, 0 before instruction reg = afh w=b 5 h after instruction reg = 1ah w=b 5 h downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 307 pic18(l)f1xk22 24.2 extended instruction set in addition to the standard 75 instructions of the pic18 instruction set, pic18(l)f1xk22 devices also provide an optional extension to the core cpu functionality. the added features include eight additional instructions that augment indirect and indexed addressing operations and the implementation of indexed literal offset addressing mode for many of the standard pic18 instructions. the additional features of the extended instruction set are disabled by default. to enable them, users must set the xinst configuration bit. the instructions in the extended set can all be classified as literal operations, which either manipulate the file select registers, or use them for indexed addressing. two of the instructions, addfsr and subfsr , each have an additional special instantiation for using fsr2. these versions ( addulnk and subulnk ) allow for automatic return after execution. the extended instructions are specifically implemented to optimize re-entrant program code (that is, code that is recursive or that uses a software stack) written in high-level languages, particularly c. among other things, they allow users working in high-level languages to perform certain operations on data structures more efficiently. these include: dynamic allocation and deallocation of software stack space when entering and leaving subroutines function pointer invocation software stack pointer manipulation manipulation of variables located in a software stack a summary of the instructions in the extended instruction set is provided in tab l e 2 4- 3 . detailed descriptions are provided in section 24.2.2 ?extended instruction set? . the opcode field descriptions in ta b l e 2 4 - 1 (page 266) apply to both the standard and extended pic18 instruction sets. 24.2.1 extended instruction syntax most of the extended instructions use indexed arguments, using one of the file select registers and some offset to specify a source or destination register. when an argument for an instruction serves as part of indexed addressing, it is enclosed in square brackets ([ ]). this is done to indicate that the argument is used as an index or offset. mpasm? assembler will flag an error if it determines that an index or offset value is not bracketed. when the extended instruction set is enabled, brackets are also used to indicate index arguments in byte- oriented and bit-oriented instructions. this is in addition to other changes in their syntax. for more details, see section 24.2.3.1 ?extended instruction syntax with standard pic18 commands? . table 24-3: extensions to the pic18 instruction set note: the instruction set extension and the indexed literal offset addressing mode were designed for optimizing applications written in c; the user may likely never use these instructions directly in assembler. the syntax for these commands is provided as a reference for users who may be reviewing code that has been generated by a compiler. note: in the past, square brackets have been used to denote optional arguments in the pic18 and earlier instruction sets. in this text and going forward, optional arguments are denoted by braces ({ }). mnemonic, operands description cycles 16-bit instruction word status affected msb lsb addfsr addulnk callw movsf movss pushl subfsr subulnk f, k k z s , f d z s , z d k f, k k add literal to fsr add literal to fsr2 and return call subroutine using wreg move z s (source) to 1st word f d (destination) 2nd word move z s (source) to 1st word z d (destination) 2nd word store literal at fsr2, decrement fsr2 subtract literal from fsr subtract literal from fsr2 and return 12 2 2 2 1 1 2 11101110 0000 1110 1111 1110 1111 1110 1110 1110 1000 1000 0000 1011 ffff 1011 xxxx 1010 1001 1001 ffkk 11kk 0001 0zzzffff 1zzz xzzz kkkk ffkk 11kk kkkkkkkk 0100 zzzz ffff zzzz zzzz kkkk kkkk kkkk none none none none none none none none downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 308 ? 2009-2016 microchip technology inc. 24.2.2 extended instruction set addfsr add literal to fsr syntax: addfsr f, k operands: 0 ? k ? 63 f ? [ 0, 1, 2 ] operation: fsr(f) + k ? fsr(f) status affected: none encoding: 1110 1000 ffkk kkkk description: the 6-bit literal k is added to the contents of the fsr specified by f. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal k process data write to fsr example: addfsr 2, 23h before instruction fsr2 = 03ffh after instruction fsr2 = 0422h addulnk add literal to fsr2 and return syntax: addulnk k operands: 0 ? k ? 63 operation: fsr2 + k ? fsr2, (tos) ?? pc status affected: none encoding: 1110 1000 11kk kkkk description: the 6-bit literal k is added to the contents of fsr2. a return is then executed by loading the pc with the tos. the instruction takes two cycles to execute; a nop is performed during the second cycle. this may be thought of as a special case of the addfsr instruction, where f = 3 (binary 11 ); it operates only on fsr2. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal k process data write to fsr no operation no operation no operation no operation example: addulnk 23h before instruction fsr2 = 03ffh pc = 0100h after instruction fsr2 = 0422h pc = (tos) note: all pic18 instructions may take an optional label argument preceding the instruction m nemonic for use in symbolic addressing. if a label is used, the instruction syntax then becomes: {label } instruction argument(s). downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 309 pic18(l)f1xk22 callw subroutine call using wreg syntax: callw operands: none operation: (pc + 2) ? tos, (w) ? pcl, (pclath) ? pch, (pclatu) ? pcu status affected: none encoding: 0000 0000 0001 0100 description first, the return address (pc + 2) is pushed onto the return stack. next, the contents of w are written to pcl; the existing value is discarded. then, the contents of pclath and pclatu are latched into pch and pcu, respectively. the second cycle is executed as a nop instruction while the new next instruction is fetched. unlike call , there is no option to update w, status or bsr. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read wreg push pc to stack no operation no operation no operation no operation no operation example : here callw before instruction pc = address (here) pclath = 10h pclatu = 00h w = 06h after instruction pc = 001006h tos = address (here + 2) pclath = 10h pclatu = 00h w = 06h movsf move indexed to f syntax: movsf [z s ], f d operands: 0 ? z s ? 127 0 ? f d ? 4095 operation: ((fsr2) + z s ) ? f d status affected: none encoding: 1st word (source) 2nd word (destin.) 11101111 1011ffff 0zzzffff zzzz s ffff d description: the contents of the source register are moved to destination register f d . the actual address of the source register is determined by adding the 7-bit literal offset z s in the first word to the value of fsr2. the address of the destination register is specified by the 12-bit literal f d in the second word. both addresses can be anywhere in the 4096-byte data space (000h to fffh). the movsf instruction cannot use the pcl, tosu, tosh or tosl as the destination register. if the resultant source address points to an indirect addressing register, the value returned will be 00h. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode determine source addr determine source addr read source reg decode no operation no dummy read no operation write register f (dest) example : movsf [05h], reg2 before instruction fsr2 = 80h contents of 85h = 33h reg2 = 11h after instruction fsr2 = 80h contents of 85h = 33h reg2 = 33h downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 310 ? 2009-2016 microchip technology inc. movss move indexed to indexed syntax: movss [z s ], [z d ] operands: 0 ? z s ? 127 0 ? z d ? 127 operation: ((fsr2) + z s ) ? ((fsr2) + z d ) status affected: none encoding: 1st word (source) 2nd word (dest.) 11101111 1011xxxx 1zzzxzzz zzzz s zzzz d description the contents of the source register are moved to the destination register. the addresses of the source and destination registers are determined by adding the 7-bit literal offsets z s or z d , respectively, to the value of fsr2. both registers can be located anywhere in the 4096-byte data memory space (000h to fffh). the movss instruction cannot use the pcl, tosu, tosh or tosl as the destination register. if the resultant source address points to an indirect addressing register, the value returned will be 00h. if the resultant destination address points to an indirect addressing register, the instruction will execute as a nop . words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode determine source addr determine source addr read source reg decode determine dest addr determine dest addr write to dest reg example : movss [05h], [06h] before instruction fsr2 = 80h contents of 85h = 33h contents of 86h = 11h after instruction fsr2 = 80h contents of 85h = 33h contents of 86h = 33h pushl store literal at fsr2, decrement fsr2 syntax: pushl k operands: 0 ??? k ? 255 operation: k ? (fsr2), fsr2 C 1 ? fsr2 status affected: none encoding: 1110 1010 kkkk kkkk description: the 8-bit literal k is written to the data memory address specified by fsr2. fsr2 is decremented by 1 after the operation. this instruction allows users to push values onto a software stack. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read k process data write to destination example : pushl 08h before instruction fsr2h:fsr2l = 01ech memory (01ech) = 00h after instruction fsr2h:fsr2l = 01ebh memory (01ech) = 08h downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 311 pic18(l)f1xk22 subfsr subtract literal from fsr syntax: subfsr f, k operands: 0 ? k ? 63 f ? [ 0, 1, 2 ] operation: fsr(f) C k ? fsrf status affected: none encoding: 1110 1001 ffkk kkkk description: the 6-bit literal k is subtracted from the contents of the fsr specified by f. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example : subfsr 2, 23h before instruction fsr2 = 03ffh after instruction fsr2 = 03dch subulnk subtract literal from fsr2 and return syntax: subulnk k operands: 0 ? k ? 63 operation: fsr2 C k ? fsr2 (tos) ?? pc status affected: none encoding: 1110 1001 11kk kkkk description: the 6-bit literal k is subtracted from the contents of the fsr2. a return is then executed by loading the pc with the tos. the instruction takes two cycles to execute; a nop is performed during the second cycle. this may be thought of as a special case of the subfsr instruction, where f = 3 (binary 11 ); it operates only on fsr2. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination no operation no operation no operation no operation example : subulnk 23h before instruction fsr2 = 03ffh pc = 0100h after instruction fsr2 = 03dch pc = (tos) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 312 ? 2009-2016 microchip technology inc. 24.2.3 byte-oriented and bit-oriented instructions in indexed literal offset mode in addition to eight new commands in the extended set, enabling the extended instruction set also enables indexed literal offset addressing mode ( section 3.5.1 ?indexed addressing with literal offset? ). this has a significant impact on the way that many commands of the standard pic18 instruction set are interpreted. when the extended set is disabled, addresses embedded in opcodes are treated as literal memory locations: either as a location in the access bank (a = 0 ), or in a gpr bank designated by the bsr (a = 1 ). when the extended instruction set is enabled and a = 0 , however, a file register argument of 5fh or less is interpreted as an offset from the pointer value in fsr2 and not as a literal address. for practical purposes, this means that all instructions that use the access ram bit as an argument C that is, all byte-oriented and bit- oriented instructions, or almost half of the core pic18 instructions C may behave differently when the extended instruction set is enabled. when the content of fsr2 is 00h, the boundaries of the access ram are essentially remapped to their original values. this may be useful in creating backward compatible code. if this technique is used, it may be necessary to save the value of fsr2 and restore it when moving back and forth between c and assembly routines in order to preserve the stack pointer. users must also keep in mind the syntax requirements of the extended instruction set (see section 24.2.3.1 ?extended instruction syntax with standard pic18 commands? ). although the indexed literal offset addressing mode can be very useful for dynamic stack and pointer manipulation, it can also be very annoying if a simple arithmetic operation is carried out on the wrong register. users who are accustomed to the pic18 programming must keep in mind that, when the extended instruction set is enabled, register addresses of 5fh or less are used for indexed literal offset addressing. representative examples of typical byte-oriented and bit-oriented instructions in the indexed literal offset addressing mode are provided on the following page to show how execution is affected. the operand conditions shown in the examples are applicable to all instructions of these types. 24.2.3.1 extended instruction syntax with standard pic18 commands when the extended instruction set is enabled, the file register argument, f, in the standard byte-oriented and bit-oriented commands is replaced with the literal offset value, k. as already noted, this occurs only when f is less than or equal to 5fh. when an offset value is used, it must be indicated by square brackets ([ ]). as with the extended instructions, the use of brackets indicates to the compiler that the value is to be interpreted as an index or an offset. omitting the brackets, or using a value greater than 5fh within brackets, will generate an error in the mpasm? assembler. if the index argument is properly bracketed for indexed literal offset addressing, the access ram argument is never specified; it will automatically be assumed to be 0 . this is in contrast to standard operation (extended instruction set disabled) when a is set on the basis of the target address. declaring the access ram bit in this mode will also generate an error in the mpasm assembler. the destination argument, d, functions as before. in the latest versions of the mpasm assembler, language support for the extended instruction set must be explicitly invoked. this is done with either the command line option, /y , or the pe directive in the source listing. 24.2.4 considerations when enabling the extended instruction set it is important to note that the extensions to the instruction set may not be beneficial to all users. in particular, users who are not writing code that uses a software stack may not benefit from using the extensions to the instruction set. additionally, the indexed literal offset addressing mode may create issues with legacy applications written to the pic18 assembler. this is because instructions in the legacy code may attempt to address registers in the access bank below 5fh. since these addresses are interpreted as literal offsets to fsr2 when the instruction set extension is enabled, the application may read or write to the wrong data addresses. when porting an application to the pic18(l)f1xk22, it is very important to consider the type of code. a large, re-entrant application that is written in c and would benefit from efficient compilation will do well when using the instruction set extensions. legacy applications that heavily use the access bank will most likely not benefit from using the extended instruction set. note: enabling the pic18 instruction set extension may cause legacy applications to behave erratically or fail entirely. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 313 pic18(l)f1xk22 addwf add w to indexed (indexed literal offset mode) syntax: addwf [k] {,d} operands: 0 ? k ? 95 d ? [ 0 , 1 ] operation: (w) + ((fsr2) + k) ? dest status affected: n, ov, c, dc, z encoding: 0010 01d0 kkkk kkkk description: the contents of w are added to the contents of the register indicated by fsr2, offset by the value k. if d is 0 , the result is stored in w. if d is 1 , the result is stored back in register f (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read k process data write to destination example : addwf [ofst] , 0 before instruction w = 17h ofst = 2ch fsr2 = 0a00h contents of 0a2ch = 20h after instruction w = 37h contents of 0a2ch = 20h bsf bit set indexed (indexed literal offset mode) syntax: bsf [k], b operands: 0 ? f ? 95 0 ? b ? 7 operation: 1 ? ((fsr2) + k) status affected: none encoding: 1000 bbb0 kkkk kkkk description: bit b of the register indicated by fsr2, offset by the value k, is set. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register f process data write to destination example : bsf [flag_ofst], 7 before instruction flag_ofst = 0ah fsr2 = 0a00h contents of 0a0ah = 55h after instruction contents of 0a0ah = d5h setf set indexed (indexed literal offset mode) syntax: setf [k] operands: 0 ? k ? 95 operation: ffh ? ((fsr2) + k) status affected: none encoding: 0110 1000 kkkk kkkk description: the contents of the register indicated by fsr2, offset by k, are set to ffh. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read k process data write register example : setf [ofst] before instruction ofst = 2ch fsr2 = 0a00h contents of 0a2ch = 00h after instruction contents of 0a2ch = ffh downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 314 ? 2009-2016 microchip technology inc. 24.2.5 special considerations with microchip mplab ? ide tools the latest versions of microchips software tools have been designed to fully support the extended instruction set of the pic18(l)f1xk22 family of devices. this includes the mplab ? c18 c compiler, mpasm assembly language and mplab integrated development environment (ide). when selecting a target device for software development, mplab ide will automatically set default configuration bits for that device. the default setting for the xinst configuration bit is 0 , disabling the extended instruction set and indexed literal offset addressing mode. for proper execution of applications developed to take advantage of the extended instruction set, xinst must be set during programming. to develop software for the extended instruction set, the user must enable support for the instructions and the indexed addressing mode in their language tool(s). depending on the environment being used, this may be done in several ways: a menu option, or dialog box within the environment, that allows the user to configure the language tool and its settings for the project a command line option a directive in the source code these options vary between different compilers, assemblers and development environments. users are encouraged to review the documentation accompanying their development systems for the appropriate information. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 315 pic18(l)f1xk22 25.0 development support the pic ? microcontrollers (mcu) and dspic ? digital signal controllers (dsc) are supported with a full range of software and hardware development tools: integrated development environment - mplab ? x ide software compilers/assemblers/linkers - mplab xc compiler - mpasm tm assembler -mplink tm object linker/ mplib tm object librarian - mplab assembler/linker/librarian for various device families simulators - mplab x sim software simulator emulators - mplab real ice? in-circuit emulator in-circuit debuggers/programmers - mplab icd 3 - pickit? 3 device programmers - mplab pm3 device programmer low-cost demonstration/development boards, evaluation kits and starter kits third-party development tools 25.1 mplab x integrated development environment software the mplab x ide is a single, unified graphical user interface for microchip and third-party software, and hardware development tool that runs on windows ? , linux and mac os ? x. based on the netbeans ide, mplab x ide is an entirely new ide with a host of free software components and plug-ins for high- performance application development and debugging. moving between tools and upgrading from software simulators to hardware debugging and programming tools is simple with the seamless user interface. with complete project management, visual call graphs, a configurable watch window and a feature-rich editor that includes code completion and context menus, mplab x ide is flexible and friendly enough for new users. with the ability to support multiple tools on multiple projects with simultaneous debugging, mplab x ide is also suitable for the needs of experienced users. feature-rich editor: color syntax highlighting smart code completion makes suggestions and provides hints as you type automatic code formatting based on user-defined rules live parsing user-friendly, customizable interface: fully customizable interface: toolbars, toolbar buttons, windows, window placement, etc. call graph window project-based workspaces: multiple projects multiple tools multiple configurations simultaneous debugging sessions file history and bug tracking: local file history feature built-in support for bugzilla issue tracker downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 316 ? 2009-2016 microchip technology inc. 25.2 mplab xc compilers the mplab xc compilers are complete ansi c compilers for all of microchips 8, 16, and 32-bit mcu and dsc devices. these compilers provide powerful integration capabilities, superior code optimization and ease of use. mplab xc compilers run on windows, linux or mac os x. for easy source level debugging, the compilers provide debug information that is optimized to the mplab x ide. the free mplab xc compiler editions support all devices and commands, with no time or memory restrictions, and offer sufficient code optimization for most applications. mplab xc compilers include an assembler, linker and utilities. the assembler generates relocatable object files that can then be archived or linked with other relocatable object files and archives to create an executable file. mplab xc compiler uses the assembler to produce its object file. notable features of the assembler include: support for the entire device instruction set support for fixed-point and floating-point data command-line interface rich directive set flexible macro language mplab x ide compatibility 25.3 mpasm assembler the mpasm assembler is a full-featured, universal macro assembler for pic10/12/16/18 mcus. the mpasm assembler generates relocatable object files for the mplink object linker, intel ? standard hex files, map files to detail memory usage and symbol reference, absolute lst files that contain source lines and generated machine code, and coff files for debugging. the mpasm assembler features include: integration into mplab x ide projects user-defined macros to streamline assembly code conditional assembly for multipurpose source files directives that allow complete control over the assembly process 25.4 mplink object linker/ mplib object librarian the mplink object linker combines relocatable objects created by the mpasm assembler. it can link relocatable objects from precompiled libraries, using directives from a linker script. the mplib object librarian manages the creation and modification of library files of precompiled code. when a routine from a library is called from a source file, only the modules that contain that routine will be linked in with the application. this allows large libraries to be used efficiently in many different applications. the object linker/library features include: efficient linking of single libraries instead of many smaller files enhanced code maintainability by grouping related modules together flexible creation of libraries with easy module listing, replacement, deletion and extraction 25.5 mplab assembler, linker and librarian for various device families mplab assembler produces relocatable machine code from symbolic assembly language for pic24, pic32 and dspic dsc devices. mplab xc compiler uses the assembler to produce its object file. the assembler generates relocatable object files that can then be archived or linked with other relocatable object files and archives to create an executable file. notable features of the assembler include: support for the entire device instruction set support for fixed-point and floating-point data command-line interface rich directive set flexible macro language mplab x ide compatibility downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 317 pic18(l)f1xk22 25.6 mplab x sim software simulator the mplab x sim software simulator allows code development in a pc-hosted environment by simulating the pic mcus and dspic dscs on an instruction level. on any given instruction, the data areas can be examined or modified and stimuli can be applied from a comprehensive stimulus controller. registers can be logged to files for further run-time analysis. the trace buffer and logic analyzer display extend the power of the simulator to record and track program execution, actions on i/o, most peripherals and internal registers. the mplab x sim software simulator fully supports symbolic debugging using the mplab xc compilers, and the mpasm and mplab assemblers. the software simulator offers the flexibility to develop and debug code outside of the hardware laboratory environment, making it an excellent, economical software development tool. 25.7 mplab real ice in-circuit emulator system the mplab real ice in-circuit emulator system is microchips next generation high-speed emulator for microchip flash dsc and mcu devices. it debugs and programs all 8, 16 and 32-bit mcu, and dsc devices with the easy-to-use, powerful graphical user interface of the mplab x ide. the emulator is connected to the design engineers pc using a high-speed usb 2.0 interface and is connected to the target with either a connector compatible with in-circuit debugger systems (rj-11) or with the new high-speed, noise tolerant, low- voltage differential signal (lvds) interconnection (cat5). the emulator is field upgradable through future firmware downloads in mplab x ide. mplab real ice offers significant advantages over competitive emulators including full-speed emulation, run-time variable watches, trace analysis, complex breakpoints, logic probes, a ruggedized probe interface and long (up to three meters) interconnection cables. 25.8 mplab icd 3 in-circuit debugger system the mplab icd 3 in-circuit debugger system is microchips most cost-effective, high-speed hardware debugger/programmer for microchip flash dsc and mcu devices. it debugs and programs pic flash microcontrollers and dspic dscs with the powerful, yet easy-to-use graphical user interface of the mplab ide. the mplab icd 3 in-circuit debugger probe is connected to the design engineers pc using a high- speed usb 2.0 interface and is connected to the target with a connector compatible with the mplab icd 2 or mplab real ice systems (rj-11). mplab icd 3 supports all mplab icd 2 headers. 25.9 pickit 3 in-circuit debugger/ programmer the mplab pickit 3 allows debugging and programming of pic and dspic flash microcontrollers at a most affordable price point using the powerful graphical user interface of the mplab ide. the mplab pickit 3 is connected to the design engineers pc using a full-speed usb interface and can be connected to the target via a microchip debug (rj-11) connector (compatible with mplab icd 3 and mplab real ice). the connector uses two device i/o pins and the reset line to implement in-circuit debugging and in-circuit serial programming? (icsp?). 25.10 mplab pm3 device programmer the mplab pm3 device programmer is a universal, ce compliant device programmer with programmable voltage verification at v ddmin and v ddmax for maximum reliability. it features a large lcd display (128 x 64) for menus and error messages, and a modular, detachable socket assembly to support various package types. the icsp cable assembly is included as a standard item. in stand-alone mode, the mplab pm3 device programmer can read, verify and program pic devices without a pc connection. it can also set code protection in this mode. the mplab pm3 connects to the host pc via an rs-232 or usb cable. the mplab pm3 has high-speed communications and optimized algorithms for quick programming of large memory devices, and incorporates an mmc card for file storage and data applications. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 318 ? 2009-2016 microchip technology inc. 25.11 demonstration/development boards, evaluation kits, and starter kits a wide variety of demonstration, development and evaluation boards for various pic mcus and dspic dscs allows quick application development on fully functional systems. most boards include prototyping areas for adding custom circuitry and provide application firmware and source code for examination and modification. the boards support a variety of features, including leds, temperature sensors, switches, speakers, rs-232 interfaces, lcd displays, potentiometers and additional eeprom memory. the demonstration and development boards can be used in teaching environments, for prototyping custom circuits and for learning about various microcontroller applications. in addition to the picdem? and dspicdem? demonstration/development board series of circuits, microchip has a line of evaluation kits and demonstration software for analog filter design, k ee l oq ? security ics, can, irda ? , powersmart battery management, seeval ? evaluation system, sigma-delta adc, flow rate sensing, plus many more. also available are starter kits that contain everything needed to experience the specified device. this usually includes a single application and debug capability, all on one board. check the microchip web page ( www.microchip.com ) for the complete list of demonstration, development and evaluation kits. 25.12 third-party development tools microchip also offers a great collection of tools from third-party vendors. these tools are carefully selected to offer good value and unique functionality. device programmers and gang programmers from companies, such as softlog and ccs software tools from companies, such as gimpel and trace systems protocol analyzers from companies, such as saleae and total phase demonstration boards from companies, such as mikroelektronika, digilent ? and olimex embedded ethernet solutions from companies, such as ez web lynx, wiznet and iplogika ? downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 319 pic18(l)f1xk22 26.0 electrical specifications 26.1 absolute maximum ratings (?) ambient temperature under bias................................................................................................. ..... -40c to +125c storage temperature ............................................................................................................ ............ -65c to +150c voltage on pins with respect to v ss on v dd pin pic18f1xk22 ........................................................................................................... -0.3v t o +6.5v pic18lf1xk22 ......................................................................................................... -0.3v to +4.0v on mclr pin .......................................................................................................................... . -0.3v to +9.0v on all other pins ............................................................................................................ -0.3v to (v dd + 0.3v) maximum current (1) on v ss pin -40c ? t a ? +85c, industrial ............................................................................................. 250 ma -40c ? t a ? +125c, extended ............................................................................................. 85 ma on v dd pin -40c ? t a ? +85c,industrial .............................................................................................. 250 ma -40c ? t a ? +125c, extended ............................................................................................. 85 ma sunk by all ports.............................................................................................................. ..................... 250 ma sourced by all ports ............................................................................................................................. 250 ma maximum output current sunk by any i/o pin.............................................................................................................................. ? 50 ma sourced by any i/o pin ........................................................................................................ ............... ? 50 ma clamp current, i k (v pin < 0 or v pin > v dd ) ................................................................................................... ? 20 ma total power dissipation (2) ............................................................................................................................... 800 mw note 1: maximum current rating requires even load distribution across i/o pins. maximu m current rating may be limited by the device package power dissipation characterizations, see tab le 2 6- 8 to calculate device specifications. 2: power dissipation is calculated as follows: p dis = v dd x {i dd C i oh } + {v dd C v oh ) x i oh } + (v ol x i o i). ? notice: stresses above those listed under absolute maximum ratings may cause permanent damage to the device. this is a stress rating only and functional operation of the device at those or any other conditions abov e those indicated in the operation listings of this specification is not implied. exposure above maximum rating conditions for extended periods may affect device reliability. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 320 ? 2009-2016 microchip technology inc. 26.2 standard operating conditions the standard operating conditions for any device are defined as: operating voltage: v ddmin ?? v dd ?? v ddmax operating temperature: t a _ min ?? t a ?? t a _ max v dd ? operating supply voltage (1) pic18lf1xk22 v ddmin (fosc ? 16 mhz).......................................................................................................... +1.8v v ddmin (fosc ? 20 mhz).......................................................................................................... +2.0v v ddmin (fosc ? 64 mhz).......................................................................................................... +3.0v v ddmax ............................................................................................................................... ..... +3.6v pic18f1xk22 v ddmin (fosc ? 20 mhz).......................................................................................................... +2.3v v ddmin (fosc ? 64 mhz).......................................................................................................... +3.0v v ddmax ............................................................................................................................... ..... +5.5v t a ? operating ambient temperature range industrial temperature t a _ min ............................................................................................................................... ....... -40c t a _ max ............................................................................................................................... ..... +85c extended temperature t a _ min ............................................................................................................................... ....... -40c t a _ max ............................................................................................................................... ... +125c note 1: see parameter d001 , dc characteristics: supply voltage. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 321 pic18(l)f1xk22 figure 26-1: pic18f1xk22 volt age frequency graph, -40c ?? t a ?? +85c figure 26-2: pic18f1xk22 volt age frequency graph, -40c ?? t a ?? +125c 0 frequency (mhz) v dd (v) note 1: the shaded region indicates the permissible combinations of voltage and frequency. 2: refer to table 26-13 for each oscillator modes supported frequencies. 10 48 20 40 5.5 3.6 3.0 64 2.3 2.3 0 frequency (mhz) v dd (v) note 1: the shaded region indicates the permissible combinations of voltage and frequency. 2: refer to table 26-13 for each oscillator modes supported frequencies. 10 48 20 40 5.5 3.6 3.0 64 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 322 ? 2009-2016 microchip technology inc. figure 26-3: pic18lf1xk22 voltage frequency graph, -40c ?? t a ?? +85c figure 26-4: pic18lf1xk22 voltage frequency graph, -40c ?? t a ?? +125c 1.8 3.0 0 frequency (mhz) v dd (v) note 1: the shaded region indicates the permissible combinations of voltage and frequency. 2: refer to table 26-13 for each oscillator modes supported frequencies. 10 48 20 40 3.6 64 2.0 16 1.8 3.0 0 frequency (mhz) v dd (v) note 1: the shaded region indicates the permissible combinations of voltage and frequency. 2: refer to table 26-13 for each oscillator modes supported frequencies. 10 48 20 40 3.6 64 2.0 16 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 323 pic18(l)f1xk22 figure 26-5: hfintosc frequency accuracy over device v dd and temperature 125 25 2.0 0 60 85 v dd (v) 4.0 5.0 4.5 temperature ( c ) 2.5 3.0 3.5 5.5 1.8 -40 -20 5% 2% 5% 3% downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 324 ? 2009-2016 microchip technology inc. 26.3 dc characteristics table 26-1: supply voltage pic18lf1xk22 standard operating conditions (unless otherwise stated) pic18f1xk22 standard operating conditions (unless otherwise stated) param. no. sym. characteristic min. typ.? max. units conditions d001 v dd supply voltage pic18lf1xk22 1.8 2.0 3.0 3.0 3.6 3.6 3.6 3.6 vv v v f osc ? 16 mhz f osc ?? 20 mhz f osc ? 64 mhz ? 85c f osc ? 48 mhz ? 125c d001 pic18f1xk22 2.3 3.0 3.0 5.5 5.5 5.5 vv v f osc ? 20 mhz f osc ? 64 mhz ? 85c f osc ? 48 mhz ? 125c d002* v dr ram data retention voltage (1) pic18lf1xk22 1.5 v device in sleep mode d002* pic18f1xk22 1.7 v device in sleep mode v por * power-on reset release voltage 1 . 6 v v porr * power-on reset rearm voltage 0 . 8 v d004* s vdd v dd rise rate to ensure internal power-on reset signal 0.05 v/ms * these parameters are characterized but not tested. ? data in typ column is at 3.0v, 25c unless otherwise st ated. these parameters are for design guidance only and are not tested. note 1: this is the limit to which v dd can be lowered in sleep mode without losing ram data. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 325 pic18(l)f1xk22 figure 26-6: por and por rearm with slow rising v dd v dd v por v porr v ss v ss npor t por (3) por rearm note 1: when npor is low, the device is held in reset. 2: t por 1 ? s typical. 3: t vlow 2.7 ? s typical. t vlow (2) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 326 ? 2009-2016 microchip technology inc. table 26-2: rc run supply current pic18lf1xk22 standard operating conditions (unless otherwise stated) pic18f1xk22 standard operating conditions (unless otherwise stated) param. no. device characteristics typ. max. units conditions d008 supply current (i dd ) (1, 2, 4, 5) 69 ? a -40c v dd = 1.8v f osc = 31 khz (4) ( rc_run mode, lfintosc source) 71 0 ? a+ 2 5 c 81 4 ? a+ 8 5 c 11 17 ? a +125c d008a 11 15 ? a -40c v dd = 3.0v 12 16 ? a+ 2 5 c 13 25 ? a+ 8 5 c 17 28 ? a +125c d008 22 45 ? a -40c v dd = 2.3v f osc = 31 khz (4) ( rc_run mode, lfintosc source) 23 48 ? a +25c 25 50 ? a +85c 28 55 ? a +125c d008a 25 50 ? a -40c v dd = 3.0v 27 55 ? a +25c 30 60 ? a +85c 32 75 ? a +125c d008b 30 55 ? a -40c v dd = 5.0v 33 60 ? a +25c 37 65 ? a +85c 40 80 ? a +125c d009 0.4 0.5 ma -40c to +125c v dd = 1.8v f osc = 1 mhz ( rc_run mode, hfintosc source) d009a 0.6 0.8 ma -40c to +125c v dd = 3.0v d009 0.45 0.55 ma -40c to +125c v dd = 2.3v f osc = 1 mhz ( rc_run mode, hfintosc source) d009a 0.60 0.82 ma -40c to +125c v dd = 3.0v d009b 0.80 1.0 ma -40c to +125c v dd = 5.0v d010 1.9 2.5 ma -40c to +125c v dd = 1.8v f osc = 16 mhz ( rc_run mode, hf-intosc source) d010a 3.5 4.4 ma -40c to +125c v dd = 3.0v d010 2.4 3.5 ma -40c to +125c v dd = 2.3v f osc = 16 mhz ( rc_run mode, hf-intosc source) d010a 3.5 4.6 ma -40c to +125c v dd = 3.0v d010b 3.7 4.7 ma -40c to +125c v dd = 5.0v * these parameters are characterized but not tested. note 1: the test conditions for all i dd measurements in active operation mode ar e: osc1 = external square wave, from rail-to-rail; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt disabled. 2: the supply current is mainly a function of the operating voltage and frequency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code execution pa ttern and temperature, also have an impact on the current consumption. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be extended by the formula i r = v dd /2r ext (ma) with r ext in k ?? 4: fvr and bor are disabled. 5: when a single temperature range is provided for a paramete r, the specification applies to both industrial and extended temperature devices. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 327 pic18(l)f1xk22 table 26-3: rc idle supply current pic18lf1xk22 standard operating conditions (unless otherwise stated) pic18f1xk22 standard operating conditions (unless otherwise stated) param. no. device characteristics typ. max. units conditions d011 supply current (i dd ) (1, 2, 4, 5) 25 ? a -40c v dd = 1.8v f osc = 31 khz (4) ( rc_idle mode, lfintosc source) 26 ? a+ 2 5 c 39 ? a+ 8 5 c 81 1 ? a +125c d011a 4 8 ? a -40c v dd = 3.0v 41 0 ? a+ 2 5 c 51 3 ? a+ 8 5 c 81 5 ? a +125c d011 20 28 ? a -40c v dd = 2.3v f osc = 31 khz (4) ( rc_idle mode, lfintosc source) 21 35 ? a +25c 23 41 ? a +85c 24 50 ? a +125c d011a 23 35 ? a -40c v dd = 3.0v 25 40 ? a +25c 28 46 ? a +85c 30 65 ? a +125c d011b 28 43 ? a -40c v dd = 5.0v 30 48 ? a +25c 32 51 ? a +85c 33 71 ? a +125c d012 0.30 0.45 ma -40c to +125c v dd = 1.8v f osc = 1 mhz ( rc_idle mode, hf-intosc source) d012a 0.45 0.60 ma -40c to +125c v dd = 3.0v d012 0.32 0.45 ma -40c to +125c v dd = 2.3v f osc = 1 mhz ( rc_idle mode, hf-intosc source) d012a 0.47 0.62 ma -40c to +125c v dd = 3.0v d012b 0.63 0.78 ma -40c to +125c v dd = 5.0v d013 0.89 1.20 ma -40c to +125c v dd = 1.8v f osc = 16 mhz ( rc_idle mode, hf-intosc source) d013a 1.45 2.00 ma -40c to +125c v dd = 3.0v d013 1.10 1.50 ma -40c to +125c v dd = 2.3v f osc = 16 mhz ( rc_idle mode, hf-intosc source) d013a 1.45 2.00 ma -40c to +125c v dd = 3.0v d013b 1.53 2.20 ma -40c to +125c v dd = 5.0v * these parameters are char acterized but not tested. note 1: the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-rail; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt disabled. 2: the supply current is mainly a function of the operating voltage and frequency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code execution pa ttern and temperature, also have an impact on the current consumption. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be extended by the formula i r = v dd /2r ext (ma) with r ext in k ?? 4: fvr and bor are disabled. 5: when a single temperature range is provided for a parameter, the specification applies to both industrial and extended temperature devices. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 328 ? 2009-2016 microchip technology inc. table 26-4: primary run supply current pic18lf1xk22 standard operating conditions (unless otherwise stated) pic18f1xk22 standard operating conditions (unless otherwise stated) param. no. device characteristics typ. max. units conditions d014 supply current (i dd ) (1, 2, 4, 5) 0.20 0.32 ma -40c to +125c v dd = 1.8v f osc = 1 mhz ( pri_run , ec med osc) d014a 0.27 0.39 ma -40c to +125c v dd = 3.0v d014 .20 .32 ma -40c to +125c v dd = 2.3v f osc = 1 mhz ( pri_run , ec med osc) d014a .27 .39 ma -40c to +125c v dd = 3.0v d014b .30 .42 ma -40c to +125c v dd = 5.0v d015 1.7 2.6 ma -40c to +125c v dd = 1.8v f osc = 16 mhz ( pri_run , ec high osc) d015a 3.0 4.2 ma -40c to +125c v dd = 3.0v d015 2.4 3.2 ma -40c to +125c v dd = 2.3v f osc = 16 mhz ( pri_run , ec high osc) d015a 3.0 4.2 ma -40c to +125c v dd = 3.0v d015b 3.3 4.4 ma -40c to +125c v dd = 5.0v d016 11.5 14.0 ma -40c to +125c v dd = 3.0v f osc = 64 mhz ( pri_run , ec high osc) d016 11.9 14.4 ma -40c to +125c v dd = 2.3v f osc = 64 mhz ( pri_run , ec high osc) d016a 12.1 14.6 ma -40c to +125c v dd = 5.0v d017 2.1 2.9 ma -40c to +125c v dd = 1.8v f osc = 4 mhz 16 mhz internal ( pri_run hs+pll ) d017a 3.1 4.0 ma -40c to +125c v dd = 3.0v d017 2.1 2.9 ma -40c to +125c v dd = 2.3v f osc = 4 mhz 16 mhz internal ( pri_run hs+pll ) d017a 3.1 4.0 ma -40c to +125c v dd = 3.0v d017b 3.3 4.5 ma -40c to +125c v dd = 5.0v d018 10 15 ma -40c to +125c v dd = 3.0v f osc = 16 mhz 64 mhz internal ( pri_run hs+pll ) d018 12.4 15.4 ma -40c to +125c v dd = 3.0v f osc = 16 mhz 64 mhz internal ( pri_run hs+pll ) d018a 12.6 15.6 ma -40c to +125c v dd = 5.0v * these parameters are characterized but not tested. note 1: the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, fr om rail-to-rail; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt disabled. 2: the supply current is mainly a function of the operating voltage and fr equency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code executio n pattern and temperature, also have an impact on the current consumption. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be extended by the formula i r = v dd /2r ext (ma) with r ext in k ?? 4: fvr and bor are disabled. 5: when a single temperature range is provided for a paramete r, the specification applies to both industrial and extended temperature devices. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 329 pic18(l)f1xk22 table 26-5: primary idle supply current pic18lf1xk22 standard operating conditions (unless otherwise stated) pic18f1xk22 standard operating conditions (unless otherwise stated) param. no. device characteristics typ. max. units conditions d019 supply current (i dd ) (1, 2, 4, 5) 70 105 ? a -40c to +125c v dd = 1.8v f osc = 1 mhz ( pri_idle mode, ec med osc) d019a 140 180 ? a -40c to +125c v dd = 3.0v d019 80 120 ? a -40c to +125c v dd = 2.3v f osc = 1 mhz ( pri_idle mode, ec med osc) d019a 140 180 ? a -40c to +125c v dd = 3.0v d019b 151 230 ? a -40c to +125c v dd = 5.0v d020 1.0 1.8 ma -40c to +125c v dd = 1.8v f osc = 16 mhz ( pri_idle mode, ec high osc) d020a 1.2 2.0 ma -40c to +125c v dd = 3.0v d020 1.0 1.8 ma -40c to +125c v dd = 2.3v f osc = 16 mhz ( pri_idle mode, ec high osc) d020a 1.2 2.0 ma -40c to +125c v dd = 3.0v d020b 1.4 2.1 ma -40c to +125c v dd = 5.0v d021 5.0 7.0 ma -40c to +125c v dd = 3.0v f osc = 64 mhz ( pri_idle mode, ec high osc) d021 5.2 6.2 ma -40c to +125c v dd = 3.0v f osc = 64 mhz ( pri_idle mode, ec high osc) d021a 5.3 6.3 ma -40c to +125c v dd = 5.0v * these parameters are characterized but not tested. note 1: the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, fr om rail-to-rail; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt disabled. 2: the supply current is mainly a function of the operating voltage and fr equency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code executio n pattern and temperature, also have an impact on the current consumption. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be extended by the formula i r = v dd /2r ext (ma) with r ext in k ?? 4: fvr and bor are disabled. 5: when a single temperature range is provided for a parameter, the specification applies to both industrial and extended temperature devices. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 330 ? 2009-2016 microchip technology inc. table 26-6: secondary run supply current pic18lf1xk22 standard operating conditions (unless otherwise stated) pic18f1xk22 standard operating conditions (unless otherwise stated) param. no. device characteristics typ. max. units conditions d022 supply current (i dd ) (1, 2, 4) 69 ? a- 4 0 c v dd = 1.8v f osc = 32 khz (3) ( sec_run mode, timer1 as clock) 61 0 ? a+ 2 5 c 71 4 ? a+ 8 5 c 11 17 ? a +125c d022a 11 15 ? a- 4 0 c v dd = 3.0v 11 16 ? a+ 2 5 c 12 25 ? a+ 8 5 c 26 28 ? a +125c d022 22 65 ? a -40c v dd = 2.3v f osc = 32 khz (3) ( sec_run mode, timer1 as clock) 23 67 ? a +25c 25 69 ? a +85c 28 75 ? a +125c d022a 25 70 ? a -40c v dd = 3.0v 27 72 ? a +25c 30 74 ? a +85c 32 77 ? a +125c d022b 30 75 ? a -40c v dd = 5.0v 32 77 ? a +25c 34 79 ? a +85c 35 83 ? a +125c * these parameters are characterized but not tested. note 1: the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, fr om rail-to-rail; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt disabled. 2: the supply current is mainly a function of the operating voltage and fr equency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code executio n pattern and temperature, also have an impact on the current consumption. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be extended by the formula i r = v dd /2r ext (ma) with r ext in k ?? 4: fvr and bor are disabled. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 331 pic18(l)f1xk22 table 26-7: secondary idle supply current pic18lf1xk22 standard operating conditions (unless otherwise stated) pic18f1xk22 standard operating conditions (unless otherwise stated) param. no. device characteristics typ. max. units conditions d023 supply current (i dd ) (1, 2, 4) 25 ? a- 4 0 c v dd = 1.8v f osc = 32 khz (3) ( sec_idle mode, timer1 as clock) 25 ? a+ 2 5 c 39 ? a+ 8 5 c 81 1 ? a +125c d023a 4 8 ? a- 4 0 c v dd = 3.0v 51 0 ? a+ 2 5 c 92 0 ? a+ 8 5 c 20 23 ? a +125c d023 20 40 ? a -40c v dd = 2.3v f osc = 32 khz (3) ( sec_idle mode, timer1 as clock) 21 41 ? a +25c 23 44 ? a +85c 24 47 ? a +125c d023a 23 45 ? a -40c v dd = 3.0v 25 47 ? a +25c 28 49 ? a +85c 30 52 ? a +125c d023b 28 50 ? a -40c v dd = 5.0v 30 54 ? a +25c 32 59 ? a +85c 33 62 ? a +125c * these parameters are characterized but not tested. note 1: the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, fr om rail-to-rail; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt disabled. 2: the supply current is mainly a function of the operating voltage and fr equency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code executio n pattern and temperature, also have an impact on the current consumption. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be extended by the formula i r = v dd /2r ext (ma) with r ext in k ?? 4: fvr and bor are disabled. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 332 ? 2009-2016 microchip technology inc. table 26-8: power-down current pic18lf1xk22 standard operating conditions (unless otherwise stated) pic18f1xk22 standard operating conditions (unless otherwise stated) param. no. device characteristics min. typ.? max. +85c max. +125c units conditions v dd note power-down base current (i pd ) (2) d027 0.034 1.0 9.0 ? a 1.8 wdt, bor, fvr, t1osc disabled, all peripherals inactive 0.071 2.0 10 ? a3 . 0 d027 17 40 55 ? a 2.3 wdt, bor, fvr and t1osc disabled, all peripherals inactive 18 43 65 ? a 3.0 20 45 80 ? a 5.0 power-down module current d028 .46 1.3 10 ? a 1.8 lpwdt current (1) .74 3.0 11 ? a3 . 0 d028 18 44 60 ? a 2.3 lpwdt current (1) 21 46 70 ? a 3.0 22 48 85 ? a 5.0 d029 12 20 28 ? a 1.8 fvr current (3) 1 4 2 2 3 0 ? a3 . 0 d029 40 65 80 ? a 2.3 fvr current (3) 50 70 85 ? a 3.0 70 120 135 ? a 5.0 d030 12 17 23 ? a 3.0 bor current (1, 3) d030 30 55 80 ? a 3.0 bor current (1, 3) 64 100 120 ? a 5.0 d031 .65 1.5 11 ? a 1.8 t1osc current (1) 0.90 4.0 12 ? a3 . 0 d031 19 45 60 ? a 2.3 t1osc current (1) 20 50 70 ? a 3.0 22 55 80 ? a 5.0 * these parameters are characterized but not tested. ? data in typ column is at 3.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: the peripheral current is the sum of the base i dd or i pd and the additional current consumed when this peripheral is enabled. the peripheral ? current can be determined by subtracting the base i dd or i pd current from this limit. max values should be used when calculating total current consumption. 2: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd . 3: fixed voltage reference is automatic ally enabled whenever the bor is enabled. 4: a/d oscillator source is f rc . downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 333 pic18(l)f1xk22 power-down module current d032 .7 1.0 9.0 ? a 1.8 a/d current (1, 4) , no conversion in progress . 83 . 0 1 0 ? a3 . 0 d032 19 42 60 ? a 2.3 a/d current (1, 4) , no conversion in progress 20 44 65 ? a 3.0 22 46 80 ? a 5.0 d033 8 30 32 ? a 1.8 comparator current, low power c1 and c2 enabled 1 1 3 2 3 5 ? a3 . 0 d033 23 55 65 ? a 2.3 comparator current, low power c1 and c2 enabled 31 65 75 ? a 3.0 33 75 95 ? a 5.0 d033a 44 110 160 ? a 1.8 comparator current, high power c1 and c2 enabled 65 130 165 ? a3 . 0 d033a 77 137 155 ? a 2.3 comparator current, high power c1 and c2 enabled 84 140 165 ? a 3.0 90 150 180 ? a 5.0 d034 13 18 33 ? a 1.8 fvr current 2 2 3 0 4 0 ? a3 . 0 d034 33 55 85 ? a 2.3 fvr current 35 80 95 ? a 3.0 48 90 120 ? a 5.0 table 26-8: power-down current (continued) pic18lf1xk22 standard operating conditions (unless otherwise stated) pic18f1xk22 standard operating conditions (unless otherwise stated) param. no. device characteristics min. typ.? max. +85c max. +125c units conditions v dd note * these parameters are char acterized but not tested. ? data in typ column is at 3.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: the peripheral current is the sum of the base i dd or i pd and the additional current consumed when this peripheral is enabled. the peripheral ? current can be determined by subtracting the base i dd or i pd current from this limit. max values should be used when calculating total current consumption. 2: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd . 3: fixed voltage reference is automatic ally enabled whenever the bor is enabled. 4: a/d oscillator source is f rc . downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 334 ? 2009-2016 microchip technology inc. table 26-9: i/o ports dc characteristics standard operating conditions (unless otherwise stated) param. no. sym. characteristic min. typ.? max. units conditions v il input low voltage i/o ports: d036 with ttl buffer v ss 0.8 v 4.5v ? v dd ? 5.5v d036a v ss 0 . 1 5 v dd v1.8v ? v dd ? 4.5v d036b v ss 0 . 2 v dd v2.0v ? v dd ? 5.5v d037 with schmitt trigger buffer v ss 0 . 2 v dd v1.8v ? v dd ? 5.5v d037a with i 2 c levels v ss 0 . 3 v dd v d037b with smbus levels v ss 0 . 8 v dd v2.7v ? v dd ? 5.5v d038 mclr v ss 0 . 2 v dd v d039 osc1 v ss 0 . 3 v dd v hs, hspll modes d039a osc1 v ss 0 . 2 v dd v ec, rc modes (1) d039b osc1 v ss 0 . 3 v dd v xt, lp modes d039c t1cki v ss 0 . 3 v dd v v ih input high voltage i/o ports: d040 with ttl buffer 2.0 v dd v4.5v ? v dd ?? 5.5v d040a 0.25 v dd + 0.8 v dd v1.8v ? v dd ? 4.5v d041 with schmitt trigger buffer 0.8 v dd v dd v1.8v ? v dd ? 5.5v d041a with i 2 c levels 0.7 v dd v dd v d037a with smbus levels 2.1 v dd v2.7v ? v dd ? 5.5v d042 mclr 0.8 v dd v dd v d042a mclr 0.9 v dd 0 . 3 v dd v1.8v ? v dd ? 2.4v d043 osc1 0.7 v dd v dd v hs, hspll modes d043a osc1 0.8 v dd v dd v ec mode d043b osc1 0.9 v dd v dd v rc mode (1) d043c osc1 1.6 v dd v xt, lp modes d043e t1cki 1.6 v dd v i il input leakage current (2) d060 i/o ports 5 5 100 1000 nana v ss ? v pin ? v dd , pin at high-impedance, -40c to 85c v ss ? v pin ? v dd , 85c to 125c d061 mclr (3) 5 0 2 0 0n a v ss ? v pin ? v dd i pur portb weak pull-up current d070* 50 250 400 ? av dd = 5.0v, v pin = v ss * these parameters are characterized but not tested. ? data in typ column is at 3.0v, 25c unless otherwise stat ed. these parameters are for design guidance only and are not tested. note 1: in rc oscillator configuration, the osc1/clkin pin is a schm itt trigger input. it is not recommended to use an external clock in rc mode. 2: negative current is defined as current sourced by the pin. 3: the leakage current on the mclr pin is strongly dependent on the applied voltage level. the specified levels represent normal operating conditions. higher leakage current may be measured at different input voltages. 4: including osc2 in clkout mode. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 335 pic18(l)f1xk22 v ol output low voltage (4) d080 i/o ports v ss +0.6 v ss +0.6 v ss +0.6 v i ol = 8 ma, v dd = 5v i ol = 6 ma, v dd = 3.3v i ol = 3 ma, v dd = v ddmin table 26-9: i/o ports (continued) dc characteristics standard operating conditions (unless otherwise stated) param. no. sym. characteristic min. typ.? max. units conditions * these parameters are char acterized but not tested. ? data in typ column is at 3.0v, 25c unless otherwise stat ed. these parameters are for design guidance only and are not tested. note 1: in rc oscillator configuration, the osc1/clkin pin is a schm itt trigger input. it is not recommended to use an external clock in rc mode. 2: negative current is defined as current sourced by the pin. 3: the leakage current on the mclr pin is strongly dependent on the applied voltage level. the specified levels represent normal operating conditions. higher leakage current may be measured at different input voltages. 4: including osc2 in clkout mode. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 336 ? 2009-2016 microchip technology inc. v oh output high voltage (4) d090 i/o ports v dd -0.7 v dd -0.7 v dd -0.7 v i oh = 3.5 ma, v dd = 5v i oh = 3 ma, v dd = 3.3v i oh = 2 ma, v dd = v ddmin capacitive loading specs on output pins d101* cosc2 osc2 pin 15 pf in xt, hs and lp modes when external clock is used to drive osc1 d101a* c io all i/o pins 50 pf table 26-9: i/o ports (continued) dc characteristics standard operating conditions (unless otherwise stated) param. no. sym. characteristic min. typ.? max. units conditions * these parameters are characterized but not tested. ? data in typ column is at 3.0v, 25c unless otherwise stat ed. these parameters are for design guidance only and are not tested. note 1: in rc oscillator configuration, the osc1/clkin pin is a schm itt trigger input. it is not recommended to use an external clock in rc mode. 2: negative current is defined as current sourced by the pin. 3: the leakage current on the mclr pin is strongly dependent on the applied voltage level. the specified levels represent normal operating conditions. higher leakage current may be measured at different input voltages. 4: including osc2 in clkout mode. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 337 pic18(l)f1xk22 table 26-10: memory pr ogramming requirements dc characteristics standard operating conditions (unless otherwise stated) param. no. sym. characteristic min. typ.? max. units conditions internal program memory programming specifications (1) d110 v pp voltage on mclr /v pp /ra3 pin 8 9 v (note 3, note 4) d113 i ddp supply current during programming 10 ma data eeprom memory (2) d120 e d byte endurance 100k e/w -40 ? c to +85 ? c d121 v drw v dd for read/write v ddmin v ddmax v using eecon to read/write d122 t dew erase/write cycle time 3 4 ms d123 t retd characteristic retention 40 year provided no other specifications are violated d124 t ref number of total erase/write cycles before refresh (2) 1m 10m e/w -40c to +85c d130 program flash memory e p cell endurance 10k e/w tem perature during program- ming: 10c ? t a ? 40c d131 v pr v dd for read v ddmin v ddmax v d131a voltage on mclr /v pp during erase/program 8.0 9.0 v temperature during program- ming: 10c ? t a ? 40c d131b v be v dd for bulk erase 2.7 v ddmax v temperature during program- ming: 10c ? t a ? 40c d132 v pew v dd for write or row erase 2.2 v ddmin v ddmax v ddmax v pic18lf1xk22 pic18f1xk22 d132a i pppgm current on mclr /v pp during erase/write 1 . 0m a temperature during program- ming: 10c ? t a ? 40c d132b i ddpgm current on v dd during erase/write 5.0 ma temperature during program- ming: 10c ? t a ? 40c d133 t pew erase/write cycle time 2.0 2.8 ms temperature during program- ming: 10c ? t a ? 40c d134 t retd characteristic retention 40 year provided no other specifications are violated ? data in typ column is at 3.0v, 25c unless otherwise st ated. these parameters are fo r design guidance only and are not tested. note 1: these specifications are for programming the on-chip program memory th rough the use of table write instructions. 2: refer to section 5.8 ?using the data eeprom? for a more detailed discussion on data eeprom endurance. 3: required only if single-supply programming is disabled. 4: the mplab icd 2 does not support variable v pp output. circuitry to limit the icd 2 v pp voltage must be placed between the icd 2 and target system when programming or debugging with the icd 2. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 338 ? 2009-2016 microchip technology inc. table 26-11: thermal characteristics standard operating conditions (unless otherwise stated) param. no. sym. characteristic typ. units conditions th01 ? ja thermal resistance junction to ambient 62.2 ? c/w 20-pin pdip package 75.0 ? c/w 20-pin soic package 89.3 ? c/w 20-pin ssop package 43.0 ? c/w 20-pin qfn 4x4mm package th02 ? jc thermal resistance junction to case 27.5 ? c/w 20-pin pdip package 23.1 ? c/w 20-pin soic package 31.1 ? c/w 20-pin ssop package 5.3 ? c/w 20-pin qfn 4x4mm package th03 t jmax maximum junction temperature 150 ? c th04 pd power dissipation w pd = p internal + p i / o th05 p internal internal power dissipation w p internal = i dd x v dd (1) th06 p i / o i/o power dissipation w p i / o = ? (i ol * v ol ) + ? (i oh * (v dd - v oh )) th07 p der derated power w p der = pd max (t j - t a )/ ? ja (2) note 1: i dd is current to run the chip alone wit hout driving any load on the output pins. 2: t a = ambient temperature. 3: t j = junction temperature. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 339 pic18(l)f1xk22 26.4 timing parameter symbology the timing parameter symbols have been created with one of the following formats: figure 26-7: load conditions 1. tpps2pps 2. tpps t f frequency t time lowercase letters (pp) and their meanings: pp cc ccp1 osc osc1 ck clkout rd rd cs cs rw rd or wr di sdi sc sck do sdo ss ss dt data in t0 t0cki io i/o port t1 t1cki mc mclr wr wr uppercase letters and their meanings: s ff a l l pp e r i o d hh i g h rr i s e i invalid (high-impedance) v valid l low z high-impedance v ss c l legend: c l = 50 pf for all pins, 15 pf for osc2 output load condition pin downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 340 ? 2009-2016 microchip technology inc. 26.5 ac characteristics: pic18(l)f1xk22-i/e figure 26-8: clock timing osc1/clkin osc2/clkout q4 q1 q2 q3 q4 q1 os02 os03 os04 os04 osc2/clkout (lp,xt,hs modes) (clkout mode) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 341 pic18(l)f1xk22 table 26-12: external cl ock timing requirements param. no. symbol characteristic min. max. units conditions 1a f osc external clkin frequency (1) dc 48 mhz ec, ecio oscillator mode, (extended range devices) dc 64 mhz ec, ecio oscillator mode, (industrial range devices) oscillator frequency (1) dc 4 mhz rc oscillator mode 0.1 4 mhz xt oscillator mode 4 25 mhz hs oscillator mode 4 16 mhz hs + pll oscillator mode, (industrial range devices) 4 12 mhz hs + pll oscillator mode, (extended range devices) 5 33 khz lp oscillator mode 1t osc external clkin period (1) 20.8 ns ec, ecio, oscillator mode (extended range devices) 15.6 ns ec, ecio, oscillator mode, (industrial range devices) oscillator period (1) 250 ns rc oscillator mode 250 10,000 ns xt oscillator mode 40 62.5 83.3 250 250 250 nsns ns hs oscillator mode hs + pll oscillator mode, (industrial range devices) hs + pll oscillator mode, (extended range devices) 30 200 ? s lp oscillator mode 2t cy instruction cycle time (1) 62.5 ns t cy = 4/f osc 3t os l, t os h external clock in (osc1) high or low time 30 ns xt oscillator mode 2.5 ? s lp oscillator mode 10 ns hs oscillator mode 4t os r, t os f external clock in (osc1) rise or fall time 20 ns xt oscillator mode 50 ns lp oscillator mode 7.5 ns hs oscillator mode note 1: instruction cycle period (t cy ) equals four times the input oscillator time base period for all configurations except pll. all specified values are based on characterization data for that particular oscillator type under standard operating conditions with the device executing code. exceeding these specified limits may result in an unstable oscillator operation and/or higher than expected current consumpti on. all devices are tested to operate at min. values with an external clock applied to the osc1/clkin pin. when an external clock input is used, the max. cycle time limit is dc (no clock) for all devices. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 342 ? 2009-2016 microchip technology inc. table 26-13: oscillator parameters standard operating conditions (unless otherwise stated) param. no. sym. characteristic freq. tolerance min. typ.? max. units conditions os08 hf osc internal calibrated hfintosc frequency (2) ? 2% ? 3% 16.0 16.0 mhzmhz 0c ? t a ? 60c 60c ? t a ? +85c ? 5% 16.0 mhz os09 lf osc internal lfintosc frequency 0 31.25 khz os10* t iosc st hfintosc wake-up from sleep start-up time 58 ? sv dd = 2.0v, -40c to +85c 58 ? sv dd = 3.0v, -40c to +85c 58 ? sv dd = 5.0v, -40c to +85c * these parameters are characterized but not tested. ? data in typ column is at 3.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: instruction cycle period (t cy ) equals four times the input oscillator time bas e period. all specified values are based on characterization data for that particular oscillator ty pe under standard operating conditions with the device executing code. exceeding these specified limits may result in an unst able oscillator operation and/or higher than expected current consumption. all devices are tested to operate at min valu es with an external clock applied to the osc1 pin. when an external clock input is used, the max cycle ti me limit is dc (no clock) for all devices. 2: to ensure these oscillator frequency tolerances, v dd and v ss must be capacitively decoupled as close to the device as possible. 0.1 ? f and 0.01 ? f values in parallel are recommended. 3: by design. table 26-14: pll clock timing specifications (v dd = 1.8v to 5.5v) param. no. sym. characteristic min. typ.? max. units conditions f10 f osc oscillator frequency range 4 5 mhz v dd = 1.8-3.0v 41 6m h z v dd = 3.0-5.0v, -40c to +85c 41 2m h z v dd = 3.0-5.0v, 125 c f11 f sys on-chip vco system frequency 16 20 mhz v dd = 1.8-3.0v 16 64 mhz v dd = 3.0-5.0v, -40c to +85c 16 48 mhz v dd = 3.0-5.0v, 125c f12 t rc pll start-up time (lock time) 2 ms f13* ? clk clkout stability (jitter) -0.25 +0.25 % * these parameters are characterized but not tested. ? data in typ column is at 3v, 25 ? c unless otherwise stated. these parameters are for design guidance only and are not tested. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 343 pic18(l)f1xk22 figure 26-9: clkout and i/o timing f osc clkout i/o pin (input) i/o pin (output) q4 q1 q2 q3 os11 os19 os13 os15 os18, os19 os20 os21 os17 os16 os14 os12 os18 old value new value write fetch read execute cycle table 26-15: clkout and i/o timing parameters standard operating conditions (unless otherwise stated) param. no. sym. characteristic min. typ.? max. units conditions os11 t os h2 ck lfosc ? to clkout ? (1) 7 0n s v dd = 3.3-5.0v os12 t os h2 ck hfosc ? to clkout ? (1) 7 2n s v dd = 3.3-5.0v os13 t ck l2 io vclkout ? to port out valid (1) 2 0n s os14 t io v2 ck h port input valid before clkout ? (1) t osc + 200 ns ns os15 t os h2 io vfosc ? (q1 cycle) to port out valid 50 70* ns v dd = 3.3-5.0v os16 t os h2 io ifosc ? (q2 cycle) to port input invalid (i/o in hold time) 50 ns v dd = 3.3-5.0v os17 t io v2 os h port input valid to fosc ?? (q2 cycle) (i/o in setup time) 20 ns os18 t io r port output rise time (2) 4015 7232 ns v dd = 1.8v v dd = 3.3-5.0v os19 t io f port output fall time (2) 2815 5530 ns v dd = 1.8v v dd = 3.3-5.0v os20* t inp int pin input high or low time 25 ns os21* t rbp portb interrupt-on-change new input level time 25 ns * these parameters are characterized but not tested. ? data in typ column is at 3.0v, 25 ? c unless otherwise stated. note 1: measurements are taken in rc mode where clkout output is 4 x t osc . 2: includes osc2 in clkout mode. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 344 ? 2009-2016 microchip technology inc. figure 26-10: reset, watchdog timer, os cillator start-up timer and power-up timer timing figure 26-11: brown-out rese t timing and characteristics v dd mclr internal por pwrt time-out osc start-up time internal reset (1) watchdog timer 33 32 30 31/ 34 i/o pins 34 note 1: asserted low. reset (1) 31a v bor v dd (device in brown-out reset) (device not in brown-out reset) 33 (1) 37 note 1: 64 ms delay only if pwrte bit in the configuration word register is programmed to 0 . 2 ms delay if pwrte = 0 . reset (due to bor) v bor and v hyst tborrej downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 345 pic18(l)f1xk22 table 26-16: reset, watchdog timer, oscill ator start-up timer, power-up timer, and brown-out reset parameters standard operating conditions (unless otherwise stated) param. no. sym. characteristic min. typ.? max. units conditions 30 t mc lmclr pulse width (low) 2 5 ? s ? s v dd = 3.3-5v, -40c to +85c v dd = 3.3-5v 31 t wdt standard watchdog timer time-out period (1:16 prescaler) 1010 1717 2730 msms v dd = 3.3v-5v, -40c to +85c v dd = 3.3v-5v 31a t wdtlp low-power watchdog timer time-out period (no prescaler) 1010 1818 2733 msms v dd = 3.3v-5v, -40c to +85c v dd = 3.3v-5v 32 t ost oscillator start-up timer period (1,2) 1024t osc (note 3) 33* t pwrt power-up timer period, pwrte = 0 40 65 140 ms 34* t ioz i/o high-impedance from mclr low or watchdog timer reset 2 . 7 3 ? s 35 v bor brown-out reset voltage 1.75 2.05 2.35 2.65 1.9 2.2 2.5 2.85 2.05 2.35 2.65 3.05 vv v v borv = 1.9v (5) borv = 2.2v (5) borv = 2.7v borv = 2.85v 36* v hyst brown-out reset hysteresis 0 25 50 mv -40c to +85c 37* t bordc brown-out reset dc response time 033 5 ? sv dd ? v bor * these parameters are characterized but not tested. ? data in typ column is at 3v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: instruction cycle period (t cy ) equals four times the input oscillator time base period. all specified valu es are based on characterization data for that particular oscillator type under standard operating condition s with the device executing code. exceeding these specified limits may result in an unstable oscillator operation and/or higher than expected current consumption. all devices are tested to operate at min values with an external clock applied to the osc1 pin. when an external clock input is used, the max cycle time limit is dc (no clock) for all devices. 2: by design. 3: period of the slower clock. 4: to ensure these voltage tolerances, v dd and v ss must be capacitively decoupled as close to the device as possible. 0.1 ? f and 0.01 ? f values in parallel are recommended. 5: pic18lf1xk22 devices only. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 346 ? 2009-2016 microchip technology inc. figure 26-12: timer0 and time r1 external clock timings t0cki t1cki 40 41 42 45 46 47 49 tmr0 or tmr1 table 26-17: timer0 and timer1 external clock requirements standard operating conditions (unless otherwise stated) param. no. sym. characteristic min. typ.? max. units conditions 40* t t 0h t0cki high-pulse width no prescaler 0.5 t cy + 20 ns with prescaler 10 ns 41* t t 0l t0cki low-pulse width no prescaler 0.5 t cy + 20 ns with prescaler 10 ns 42* t t 0p t0cki period greater of: 20 or t cy + 40 n ns n = prescale value (2, 4, ..., 256) 45* t t 1h t1cki high time synchronous, no prescaler 0.5 t cy + 20 ns synchronous, with prescaler 15 ns asynchronous 30 ns 46* t t 1l t1cki low time synchronous, no prescaler 0.5 t cy + 20 ns synchronous, with prescaler 15 ns asynchronous 30 ns 47* t t 1p t1cki input period synchronous greater of: 30 or t cy + 40 n ns n = prescale value (1, 2, 4, 8) asynchronous 60 ns 48 f t 1 timer1 oscillator input frequency range (oscillator enabled by setting bit t1os- cen) 32.4 32.76 8 33.1 khz 49* tckez tmr 1 delay from external clock edge to timer increment 2 t osc 7 t osc timers in sync mode * these parameters are characterized but not tested. ? data in typ column is at 3v, 25c unless otherwise st ated. these parameters are for design guidance only and are not tested. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 347 pic18(l)f1xk22 figure 26-13: capture/com pare/pwm timings (ccp) note: refer to figure 26-7 for load conditions. (capture mode) cc01 cc02 cc03 ccpx table 26-18: capture/compare/pwm requirements (ccp) standard operating conditions (unless otherwise stated) param. no. sym. characteristic min. typ.? max. units conditions cc01* tccl ccpx input low time no prescaler 0.5t cy + 20 ns with prescaler 20 ns cc02* tcch ccpx input high time no prescaler 0.5t cy + 20 ns with prescaler 20 ns cc03* tccp ccpx input period 3t cy + 40 n ns n = prescale value (1, 4 or 16) * these parameters are char acterized but not tested. ? data in typ column is at 3v, 25c unless otherwise stat ed. these parameters are for design guidance only and are not tested. table 26-19: pic18(l)f1xk22 a/d converter (adc) characteristics standard operating conditions (unless otherwise stated) operating temperature: tested at 25c param. no. sym. characteristic min. typ.? max. units conditions ad01 n r resolution 10 bit ad02 e il integral error 2 lsb v ref = 3.0v ad03 e dl differential error 1.5 lsb no missing codes v ref = 3.0v ad04 e off offset error 3 lsb v ref = 3.0v ad05 e gn gain error 3 lsb v ref = 3.0v ad06 v ref change in reference voltage = v ref + - v ref - (2), (3) 1.8 v dd v1.8 ?? v ref + ?? v dd + 0.3v v ss - 0.3v ?? v ref - ?? v ref + - 1.8v ad07 v ain full-scale range v ss v ref v ad08 z ain recommended impedance of analog voltage source 1 0 k ? can go higher if external 0.01 ? f capacitor is present on input pin. * these parameters are char acterized but not tested. ? data in typ column is at 3.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: total absolute error includes integral, differential, offset and gain errors. 2: adc v ref is from external v ref , v dd pin or fvr, whichever is selected as reference input. 3: fvr voltage selected must be 2.048v or 4.096v. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 348 ? 2009-2016 microchip technology inc. figure 26-14: a/d conversion timing table 26-20: a/d conversion requirements param. no. symbol characteristic min. max. units conditions 130* t ad a/d clock period 0.7 25.0 (1) ? st osc based, v ref ? 3.0v, -40c to +85c 0.7 4.0 (1) ? s t osc based, v ref ? 3.0v, -40c to +125c 1.0 4.0 ? s a/d rc mode 131 t cnv conversion time (not including acquisition time) (2) 12 12 t ad 132* t acq acquisition time (3) 1.4 5.0 ? sv dd ?? 3.0v, r s = 50 ? 135 t swc switching time from convert - sample (4) 136 t dis discharge time 2 2 ? s * these parameters are characterized but not tested. note 1: the time of the a/d clock period is dependent on the device frequency and the t ad clock divider. 2: adres register may be read on the following t cy cycle. 3: the time for the holding capacitor to acquire the new input voltage when the voltage changes full scale after the conversion (v dd to v ss or v ss to v dd ). the source impedance (r s ) on the input channels is 50. 4: on the following cycle of the device clock. 131 130 132 bsf adcon0, go q4 a/d clk a/d data adres adif go sample old_data sampling stopped done new_data (note 2) 987 21 0 note 1: if the a/d clock source is selected as rc, a time of t cy is added before the a/d clock starts. this allows the sleep instruction to be executed. 2: this is a minimal rc delay (typically 100 ns), which also disconnects the holding capacitor from the analog input. .. . . . . t cy downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 349 pic18(l)f1xk22 table 26-21: comparator specifications standard operating conditions (unless otherwise stated) param. no. sym. characteristics min. typ. max. units comments cm01 v ioff input offset voltage 10 50 mv v ref = v dd /2, high-power mode 1 2 8 0m vv ref = v dd /2, low-power mode cm02 v icm input common-mode voltage v ss v dd v cm04 t resp response time 200 400 ns high-power mode 300 600 ns low-power mode cm05 t mc 2 ov comparator mode change to output valid* 1 0 ? s * these parameters are characterized but not tested. note 1: response time measured with one comparator input at v dd /2, while the other input transitions from v ss to v dd . table 26-22: digital-to-analog co nverter (dac) specifications standard operating conditions (unless otherwise stated) param. no. sym. characteristics min. typ. max. units comments dac01* c lsb step size v dd /32 v dac02* c acc absolute accuracy ? 1/2 lsb dac03* c r unit resistor value (r) 5k ? dac04* c st settling time (1) 1 0 ? s * these parameters are characterized but not tested. note 1: settling time measured while dacr<4:0> transitions from 0000 to 1111 . table 26-23: fixed voltage re ference (fvr) specifications vr voltage reference specifications standard operating conditions (unless otherwise stated) param. no. sym. characteristics min. typ. max. units comments d003 v adfvr fixed voltage reference voltage for adc, initial accuracy -8 6 % 1.024v, v dd ? 2.5v (1) 2.048v, v dd ? 2.5v 4.096v, v dd ? 4.75v d003a v cdafvr fixed voltage reference voltage for comparator and dac, initial accuracy -11 7 % 1.024v, v dd ? 2.5v 2.048v, v dd ? 2.5v 4.096v, v dd ? 4.75v d004* s vdd v dd rise rate to ensure internal power-on reset signal 0.05 v/ms see section 22.3 ?power-on reset (por)? for details. * these parameters are characterized but not tested. note 1: for proper operation, the minimum value of the adc positive voltage reference must be 1.8v or greater. when selecting the fvr or the v ref + pin as the source of the adc positive voltage reference, be aware that the voltage must be 1.8v or greater. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 350 ? 2009-2016 microchip technology inc. figure 26-15: usart synchronous transmission (master/slave) timing figure 26-16: usart synchrono us receive (master/slave) timing note: refer to figure 26-7 for load conditions. us121 us121 us120 us122 ck dt table 26-24: usart synchronous transmission requirements standard operating conditions (unless otherwise stated) param. no. symbol characteristic min. max. units conditions us120 t ck h2 dt v sync xmit (m aster and s lave ) clock high to data-out valid 3.0-5.5v 80 ns 1.8-5.5v 100 ns us121 t ckrf clock out rise time and fall time (master mode) 3.0-5.5v 45 ns 1.8-5.5v 50 ns us122 t dtrf data-out rise time and fall time 3.0-5.5v 45 ns 1.8-5.5v 50 ns note: refer to figure 26-7 for load conditions. us125 us126 ck dt table 26-25: usart synchrono us receive requirements standard operating conditions (unless otherwise stated) param. no. symbol characteristic min. max. units conditions us125 t dt v2 ckl sync rcv (m aster and s lave ) data-hold before ck ? (dt hold time) 10 ns us126 t ck l2 dtl data-hold after ck ? (dt hold time) 15 ns downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 351 pic18(l)f1xk22 figure 26-17: spi master mode timing (cke = 0 , smp = 0 ) figure 26-18: spi master mode timing (cke = 1 , smp = 1 ) ss sck (ckp = 0 ) sck (ckp = 1 ) sdo sdi sp70 sp71 sp72 sp73 sp74 sp75, sp76 sp78 sp79 sp80 sp79 sp78 msb lsb bit 6 - - - - - -1 msb in lsb in bit 6 - - - -1 note: refer to figure 26-7 for load conditions. ss sck (ckp = 0 ) sck (ckp = 1 ) sdo sdi sp81 sp71 sp72 sp74 sp75, sp76 sp78 sp80 msb sp79 sp73 msb in bit 6 - - - - - -1 lsb in bit 6 - - - -1 lsb note: refer to figure 26-7 for load conditions. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 352 ? 2009-2016 microchip technology inc. figure 26-19: spi slav e mode timing (cke = 0 ) figure 26-20: spi slav e mode timing (cke = 1 ) ss sck (ckp = 0 ) sck (ckp = 1 ) sdo sdi sp70 sp71 sp72 sp73 sp74 sp75, sp76 sp77 sp78 sp79 sp80 sp79 sp78 msb lsb bit 6 - - - - - -1 msb in bit 6 - - - -1 lsb in sp83 note: refer to figure 26-7 for load conditions. ss sck (ckp = 0 ) sck (ckp = 1 ) sdo sdi sp70 sp71 sp72 sp82 sp74 sp75, sp76 msb bit 6 - - - - - -1 lsb sp77 msb in bit 6 - - - -1 lsb in sp80 sp83 note: refer to figure 26-7 for load conditions. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 353 pic18(l)f1xk22 figure 26-21: i 2 c bus start/stop bits timing table 26-26: spi mode requirements param. no. symbol characteristic min. typ.? max. units conditions sp70* t ss l2 sc h, t ss l2 sc l ss ? to sck ? or sck ? input t cy n s sp71* t sc h sck input high time (slave mode) t cy + 20 ns sp72* t sc l sck input low time (slave mode) t cy + 20 ns sp73* t di v2 sc h, t di v2 sc l setup time of sdi data input to sck edge 100 ns sp74* t sc h2 di l, t sc l2 di l hold time of sdi data input to sck edge 100 ns sp75* t do r sdo data output rise time 3.0-5.5v 10 25 ns 1.8-5.5v 25 50 ns sp76* t do f sdo data output fall time 10 25 ns sp77* t ss h2 do zss ? to sdo output high-impedance 10 50 ns sp78* t sc r sck output rise time (master mode) 3.0-5.5v 10 25 ns 1.8-5.5v 25 50 ns sp79* t sc f sck output fall time (master mode) 10 25 ns sp80* t sc h2 do v, t sc l2 do v sdo data output valid after sck edge 3.0-5.5v 50 ns 1.8-5.5v 145 ns sp81* t do v2 sc h, t do v2 sc l sdo data output setup to sck edge tcy ns sp82* t ss l2 do v sdo data output valid after ss ? edge 50 ns sp83* t sc h2 ss h, t sc l2 ss h ss ?? after sck edge 1.5t cy + 40 ns * these parameters are characterized but not tested. ? data in typ column is at 3.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note : refer to figure 26-7 for load conditions. sp91 sp92 sp93 scl sda start condition stop condition sp90 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 354 ? 2009-2016 microchip technology inc. figure 26-22: i 2 c bus data timing table 26-27: i 2 c bus start/stop bits requirements param. no. symbol characteristic min. typ. max. units conditions sp90* t su : sta start condition 100 khz mode 4700 ns only relevant for repeated start condition setup time 400 khz mode 600 sp91* t hd : sta start condition 100 khz mode 4000 ns after this period, the first clock pulse is generated hold time 400 khz mode 600 sp92* t su : sto stop condition 100 khz mode 4700 ns setup time 400 khz mode 600 sp93 t hd : sto stop condition 100 khz mode 4000 ns hold time 400 khz mode 600 * these parameters are characterized but not tested. note: refer to figure 26-7 for load conditions. sp90 sp91 sp92 sp100 sp101 sp103 sp106 sp107 sp109 sp109 sp110 sp102 scl sda in sda out downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 355 pic18(l)f1xk22 table 26-28: i 2 c bus data requirements param. no. symbol characteristic min. max. units conditions sp100* t high clock high time 100 khz mode 4.0 ? s device must operate at a minimum of 1.5 mhz 400 khz mode 0.6 ? s device must operate at a minimum of 10 mhz ssp module 1.5t cy sp101* t low clock low time 100 khz mode 4.7 ? s device must operate at a minimum of 1.5 mhz 400 khz mode 1.3 ? s device must operate at a minimum of 10 mhz ssp module 1.5t cy sp102* t r sda and scl rise time 100 khz mode 1000 ns 400 khz mode 20 + 0.1c b 300 ns c b is specified to be from 10-400 pf sp103* t f sda and scl fall time 100 khz mode 250 ns 400 khz mode 20 + 0.1c b 250 ns c b is specified to be from 10-400 pf sp90* t su : sta start condition setup time 100 khz mode 4.7 ? s only relevant for repeated start condition 400 khz mode 0.6 ? s sp91* t hd : sta start condition hold time 100 khz mode 4.0 ? s after this period the first clock pulse is generated 400 khz mode 0.6 ? s sp106* t hd : dat data input hold time 100 khz mode 0 ns 400 khz mode 0 0.9 ? s sp107* t su : dat data input setup time 100 khz mode 250 ns (note 2) 400 khz mode 100 ns sp92* t su : sto stop condition setup time 100 khz mode 4.7 ? s 400 khz mode 0.6 ? s sp109* t aa output valid from clock 100 khz mode 3500 ns (note 1) 400 khz mode ns sp110* t buf bus free time 100 khz mode 4.7 ? s time the bus must be free before a new transmis- sion can start 400 khz mode 1.3 ? s sp c b bus capacitive loading 400 pf * these parameters are characterized but not tested. note 1: as a transmitter, the device must provide this internal minimum delay time to bridge the undefined region (min. 300 ns) of the falling edge of scl to avoid unintended generation of start or stop conditions. 2: a fast mode (400 khz) i 2 c bus device can be used in a standard mode (100 khz) i 2 c bus system, but the requirement t su : dat ?? 250 ns must then be met. this will automatically be the case if the device does not stretch the low period of the scl signal. if such a device does stretch the low period of the scl signal, it must output the next data bit to the sda line t r max. + t su : dat = 1000 + 250 = 1250 ns (according to the standard mode i 2 c bus specification), before the scl line is released. downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 356 ? 2009-2016 microchip technology inc. 27.0 dc and ac characteristics graphs and charts the graphs and tables provided in this section are for design guidance and are not tested . in some graphs or tables, the data presented are outside specified operating range (i.e., outside specified v dd range). this is for information only and devices are ensured to operate properly only within the specified range. ? typical? represents the mean of the distribution at 25 ? c. ?maximum?, ?max.?, ?minimum? or ?min.? represents (mean + 3 ? ) or (mean - 3 ? ) respectively, where ? is a standard deviation, over each temperature range. note: the graphs and tables provided following this note are a statistical summary based on a limited number of samples and are provided for informational purposes only. the performance characteristics listed herein are not tested or guaranteed. in some graphs or tables, the data presented may be outside the specified operating range (e.g., outside specified power supply range) and therefore, outside the warranted range. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 357 pic18(l)f1xk22 figure 27-1: pic18lf1xk22 typical base i pd figure 27-2: pic18lf1xk22 typical i pd for watchdog timer -40c 25c 85c 125c 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 1.8 2 2.2 2.4 2.6 2.8 3 i pd (ua) v dd (v) typ. 25c 85c 125c 0.0 0.6 1.2 1.8 2.4 3.0 3.6 4.2 4.8 5.4 6.0 1.8 2 2.2 2.4 2.6 2.8 3 i pd (ua) v dd (v) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 358 ? 2009-2016 microchip technology inc. figure 27-3: pic18lf1xk22 typical i pd for brown-out reset figure 27-4: pic18lf1xk22 typical i pd for digital-to-analog converter (cv ref ) typ. 25c 125c 85c 0 2 4 6 8 10 12 14 16 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 i pd (ua) v dd (v) 25c 85c 125c 0 5 10 15 20 25 30 35 40 1.8 2 2.2 2.4 2.6 2.8 3 i pd (ua) v dd (v) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 359 pic18(l)f1xk22 figure 27-5: pic18lf1xk22 i comp ? typical i pd for comparator in low-power mode figure 27-6: pic18lf1xk22 i comp ? typical i pd for comparator in high-power mode -40c 25c 85c 125c 5.0 10.0 15.0 20.0 25.0 1.8 2 2.2 2.4 2.6 2.8 3 i pd (ua) v dd (v) -40c 25c 85c 125c 0 25 50 75 100 125 1.8 2 2.2 2.4 2.6 2.8 3 i pd (ua) v dd (v) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 360 ? 2009-2016 microchip technology inc. figure 27-7: pic18lf1xk22 typical rc_run 31 khz i dd figure 27-8: pic18lf1xk22 typical rc_run i dd -40c 25c 85c 125c 0 5 10 15 20 25 30 1.8 2 2.2 2.4 2.6 2.8 3 i dd (ua) v dd (v) 1 mhz 16 mhz 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 1.8 2 2.2 2.4 2.6 2.8 3 i dd (ma) v dd (v) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 361 pic18(l)f1xk22 figure 27-9: pic18lf1xk22 typical pri_run i dd (ec) figure 27-10: pic18lf1xk22 typical pri_run i dd (hs + pll) 1 mhz 16 mhz 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 1.8 2 2.2 2.4 2.6 2.8 3 i dd (ma) v dd (v) 16 mhz (4 mhz input) 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 1.8 2 2.2 2.4 2.6 2.8 3 i dd (ma) v dd (v) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 362 ? 2009-2016 microchip technology inc. figure 27-11: memlow typical base i pd figure 27-12: memlow typical i pd for watchdog timer -40c 25c 85c 125c 0 5 10 15 20 25 30 35 40 45 50 2.3 2.8 3.3 3.8 4.3 4.8 i pd (ua) v dd (v) typ. 25c 85c 125c 15 0 20.0 25.0 30.0 35.0 40.0 i pd (ua) 0.0 5.0 10.0 15 . 0 2.3 2.8 3.3 3.8 4.3 4.8 v dd (v) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 363 pic18(l)f1xk22 figure 27-13: memlow typical i pd for brown-out reset figure 27-14: memlow typical i pd for digital-to-analog converter (cv ref ) typ. 25c 125c 85c 0 10 20 30 40 50 60 70 80 3 3.2 3.4 3.6 3.8 4 4.2 4.4 4.6 4.8 5 i pd (ua) v dd (v) 25c 85c 125c 0 10 20 30 40 50 60 70 80 2.3 2.8 3.3 3.8 4.3 4.8 i pd (ua) v dd (v) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 364 ? 2009-2016 microchip technology inc. figure 27-15: memlow i comp ? typical i pd for comparator in low-power mode figure 27-16: memlow i comp ? typical i pd for comparator in high-power mode 25c 85c 125c 30.0 35.0 40.0 45.0 50.0 55.0 60.0 i pd (ua) -40c 10.0 15.0 20.0 25.0 2.3 2.8 3.3 3.8 4.3 4.8 v dd (v) -40c 25c 85c 125c 0 25 50 75 100 125 150 2.3 2.8 3.3 3.8 4.3 4.8 i pd (ua) v dd (v) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 365 pic18(l)f1xk22 figure 27-17: memlow typical rc_run 31 khz i dd figure 27-18: memlow typical rc_run i dd -40c 25c 85c 125c 0 5 10 15 20 25 30 35 40 45 50 2.3 2.8 3.3 3.8 4.3 4.8 i dd (ua) v dd (v) 16 mhz 2.5 3.0 3.5 4.0 4.5 5.0 i dd (ma) 1 mhz 0.0 0.5 1.0 1.5 2.0 2.3 2.8 3.3 3.8 4.3 4.8 v dd (v) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 366 ? 2009-2016 microchip technology inc. figure 27-19: memlow typical pri_run i dd (ec) figure 27-20: memlow typical pri_run i dd (hs + pll) 1 mhz 16 mhz 64 mhz 0 2 4 6 8 10 12 14 2.3 2.8 3.3 3.8 4.3 4.8 i dd (ma) v dd (v) 64 mhz (16 mhz input) 6 8 10 12 14 16 i dd (ma) 16 mhz (4 mhz input) 0 2 4 6 2.3 2.8 3.3 3.8 4.3 4.8 v dd (v) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 367 pic18(l)f1xk22 figure 27-21: pic18(l)f1xk22 ttl buffer typical v ih figure 27-22: pic18(l)f1xk22 schm itt trigger buffer typical v ih -40c 25c 85c 125c 0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5 1.7 1.9 2.1 1.8 2.3 2.8 3.3 3.8 4.3 4.8 5.3 min. v ih (v) v dd (v) -40c 125c min. 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 1.8 2.3 2.8 3.3 3.8 4.3 4.8 5.3 v ih (v) v dd (v) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 368 ? 2009-2016 microchip technology inc. figure 27-23: pic18(l)f1xk22 ttl buffer typical v il figure 27-24: pic18(l)f1xk22 schmitt buffer typical v il -40c 25c 85c 125c 0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5 1.7 1.9 2.1 1.8 2.3 2.8 3.3 3.8 4.3 4.8 5.3 max. v il (v) v dd (v) -40c 125c max. 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 1.8 2.3 2.8 3.3 3.8 4.3 4.8 5.3 v il (v) v dd (v) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 369 pic18(l)f1xk22 figure 27-25: memlow typical lf-intosc frequency (max./min. = 31.25 khz 15%) figure 27-26: memlow typical lf-intosc frequency (max./min. = 31.25 khz 15%) -40c 25c 85c 30.3 31.3 32.3 33.3 f requency (khz) 125c 28.3 29.3 22 . 533 . 544 . 555 . 5 f v dd (v) 2.5v 3.0v 5.5v 30.5 31.0 31.5 32.0 32.5 f requency (khz) 29.0 29.5 30.0 -40 -20 0 20 40 60 80 100 120 f temperature (c) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 370 ? 2009-2016 microchip technology inc. figure 27-27: pic18lf1xk22 typical lf-intosc frequency (max./min. = 31.25 khz 15%) figure 27-28: pic18lf1xk22 typical lf-intosc frequency (max./min. = 31.25 khz 15%) -40c 25c 85c 125c 25.3 26.3 27.3 28.3 29.3 30.3 31.3 32.3 33.3 2 2.4 2.8 3.2 3.6 frequency (khz) v dd (v) 32 0 33.0 31.0 32 . 0 h z) 2.5v 3v 3.6v 29.0 30.0 f requency (k h 27.0 28.0 f 26.0 - 4 0- 2 0 0 2 04 06 08 01 0 01 2 0 temperature (c) downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 371 pic18(l)f1xk22 figure 27-29: memlow typical v oh vs. i oh figure 27-30: memlow typical v ol vs. i ol 0 1 2 3 4 5 6 0 5 10 15 20 25 30 35 2.0v 3.0v 4.0v 5.5v v oh (v) i oh (ma) 0.0 0.5 1.0 1.5 2.0 2.5 0 5 10 15 20 25 30 1.8v 3.0 4.0v 5.5v v ol (v) i ol (ma) downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 372 ? 2009-2016 microchip technology inc. 28.0 packaging information 28.1 package marking information 20-lead pdip (300 mil) example xxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxx yywwnnn pic18f13k22 0910017 20-lead ssop (5.30 mm) example pic18f13k22 -i/ss 0910017 -e/p 3 e 3 e legend: xx...x customer-specific information y year code (last digit of calendar year) yy year code (last 2 digits of calendar year) ww week code (week of january 1 is week 01) nnn alphanumeric traceability code pb-free jedec ? designator for matte tin (sn) * this package is pb-free. the pb-free jedec designator ( ) can be found on the outer packaging for this package. note : in the event the full microchip part number cannot be marked on one line, it will be carried over to the next line, thus limiting the number of available characters for customer-specific information. 3 e 3 e downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 373 pic18(l)f1xk22 package marking information (continued) 20-lead qfn (4x4x0.9 mm) example pin 1 pin 1 pic18 e/ml 910017 legend: xx...x customer-specific information y year code (last digit of calendar year) yy year code (last 2 digits of calendar year) ww week code (week of january 1 is week 01) nnn alphanumeric traceability code pb-free jedec ? designator for matte tin (sn) * this package is pb-free. the pb-free jedec designator ( ) can be found on the outer packaging for this package. note : in the event the full microchip part number cannot be marked on one line, it will be carried over to the next line, thus limiting the number of available characters for customer-specific information. 3 e 3 e f14k22 20-lead soic (7.50 mm) example 3 e PIC18F14K22 -e/so 3 e 0910017 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 374 ? 2009-2016 microchip technology inc. 28.2 package details the following sections give the technical details of the packages. 
    
     

       !" # $% &" '  ()"&'"!&)  & #*&&  & #    + % &,  & !& - ' !! #.#&"# '#% !
&"!!#% !
&"!!! & $ #/
!#   ' !  #&   
.0 1,2 1 !' !  &  $ & " !**&"&&   !   3& '!&" &
4 # * !(
 ! ! & 
 4  
% & & # & &&
255***'
'5
4  6&! 7,8. ' !9'&! 7 7: ; 7"') %! 7  & 1, 
& &    < <  # # 4 4 !!   -  1 ! & &     < < "# &"# =#& . - - - # # 4 =#& .   > :  9  &  > - ? 
& &   9  -  9 #4 !!  >   6
9 #=#& )  ?  9* 9 #=#& )  >  :  *
 + 1 < < - n e1 note 1 d 123 a a1 a2 l e b1 b e c eb 
    * , 1 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 375 pic18(l)f1xk22 
  !"#  $     %&    $
      !" # $% &" '  ()"&'"!&)  & #*&&  & #    ' !! #.#&"# '#% !
&"!!#% !
&"!!! & $ #''
!#  - ' !  #&   
.0 1,2 1 !' !  &  $ & " !**&"&&   ! .32  %   ' !("!" *&"&&   (%%' &
"
! !   3& '!&" &
4 # * !(
 ! ! & 
 4  
% & & # & &&
255***'
'5
4  6&! 99.. ' !9'&! 7 7: ; 7"') %! 7  & ?1, :  8  &  < <  # # 4 4 !!  ?  > & #%%   < < :  =#& .  > > # # 4 =#& .  - ? :  9  &  ?   3&9  & 9    3&
& 9 .3 9 #4 !!   <  3&  @ @ >@ 9 #=#& )  < -> l l1 a2 c e b a1 a 12 note 1 e1 e d n 
    * , 1 downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 376 ? 2009-2016 microchip technology inc. note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 377 pic18(l)f1xk22 note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 378 ? 2009-2016 microchip technology inc. note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 379 pic18(l)f1xk22 note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 380 ? 2009-2016 microchip technology inc. 
  ' (  )  
#* +  ,-,-&.   '(       !" # $% &" '  ()"&'"!&)  & #*&&  & #     4 !! *! " & # - ' !  #&   
.0 1,2 1 !' !  &  $ & " !**&"&&   ! .32  %   ' !("!" *&"&&   (%%' &
"
! !   3& '!&" &
4 # * !(
 ! ! & 
 4  
% & & # & &&
255***'
'5
4  6&! 99.. ' !9'&! 7 7: ; 7"') %! 7  & 1, :  8  &  >   & #%%     ,& &4 !! - .3 :  =#& . 1, .$
! # #=#& . ?  > :  9  &  1, .$
! # #9  &  ?  > ,& &=#& ) >  - ,& &9  & 9 -   ,& & & .$
! # # a  < < d exposed pad e e2 21 n top view note 1 n l k b e d2 2 1 a a1 a3 bottom view 
    * , ?1 downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 381 pic18(l)f1xk22   3& '!&" &
4 # * !(
 ! ! & 
 4  
% & & # & &&
255***'
'5
4  downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 382 ? 2009-2016 microchip technology inc. appendix a: revision history revision a (february 2009) original data sheet for pic18(l)f1xk22 devices. revision b (04/2009) revised data sheet title; revised peripheral features section; revised table 3-1, table 3-2; revised example 15-1; revised table 21-4. revision c (10/2009) updated table 1-1; updated the electrical specifications section (figures 25-1 to 25-4; sub- sections 25.1, 25.2, 25.3, 25.4, 25.5, 25.6, 25.7, 25.8, added param no. os09 to table 25-2; added param no. d003a and note 1 to table 25-12); added graphs to the dc and ac characteristics graphs and charts section; other minor corrections. revision d (05/2010) revised section 1.3 (deleted #2); revised figure 1-1; added table 2-4; removed register eeadrh from tables 3-1 and 3-2; revised section 5 (data eeprom memory); updated example 5-2 and table 5-1; revised section 13.4.4 (enhanced pwm auto-shut- down mode); added note 4 below register 13-2; revised figure 16-1; revised equation 20-1; removed sub-section 20.1.3 (output clamped to v ss ); updated figure 20-1; revised tables 21-4 and table 22-1; updated register 22-5, figure 25-5, table 25-2, table 25-8, table 25-10 and table 25-12; updated the electrical specification section; other minor corrections. revision e (10/2011) updated data sheet to new format; updated the pin diagrams; updated the electrical specifications section; updated the packaging information section; updated table b-1; updated the product identification system section; other minor corrections. revision f (04/2016) updated analog features section on page 1; updated tables 1-2, 3-2, 8-5, 8-6, 16-2 and 22-4; added note 3 to tables 3-2, 8-1 and 8-2; added note 1 to tables 9-1, 10-2, 12-1 and 17-2, and register 8-4; updated figures 3-7, 9-1 and 9-2; updated registers 13-2, 16-2, 19-1; updated section 1.1.2, 7.9 and 8.1; replaced chapter 20.0 (voltage references) with chapter 20.0 (fixed voltage reference) and 21.0 (digital-to-analog converter (dac) module); updated chapter 26.0 (electrical specifications); other minor corrections. downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 383 pic18(l)f1xk22 appendix b: device differences the differences between the devices listed in this data sheet are shown in tab l e . table b-1: device differences features pic18f13k22 PIC18F14K22 pic18lf13k22 pic18lf14k22 program memory (bytes) 8192 16384 8192 16384 program memory (instructions) 4096 8192 4096 8192 data memory sram (bytes) 256 512 256 512 data memory eeprom (bytes) 256 256 256 256 v dd min (v) 2.3 2.3 1.8 1.8 v dd max (v) 5.5 5.5 3.6 3.6 packages 20-pin pdip 20-pin soic 20-pin ssop 20-pin qfn 20-pin pdip 20-pin soic 20-pin ssop 20-pin qfn 20-pin pdip 20-pin soic 20-pin ssop 20-pin qfn 20-pin pdip 20-pin soic 20-pin ssop 20-pin qfn downloaded from: http:///
pic18(l)f1xk22 ds40001365f-page 384 ? 2009-2016 microchip technology inc. the microchip website microchip provides online support via our www site at www.microchip.com . this website is used as a means to make files and information easily available to customers. accessible by using your favorite internet browser, the website contains the following information: product support C data sheets and errata, application notes and sample programs, design resources, users guides and hardware support documents, latest software releases and archived software general technical support C frequently asked questions (faq), technical support requests, online discussion groups, microchip consultant program member listing business of microchip C product selector and ordering guides, latest microchip press releases, listing of seminars and events, listings of microchip sales offices, distributors and factory representatives customer change notification service microchips customer notification service helps keep customers current on microchip products. subscribers will receive e-mail notification whenever there are changes, updates, revisions or errata related to a specified product family or development tool of interest. to register, access the microchip website at www.microchip.com . under support, click on customer change notification and follow the registration instructions. customer support users of microchip products can receive assistance through several channels: distributor or representative local sales office field application engineer (fae) technical support customers should contact their distributor, representative or field application engineer (fae) for support. local sales offices are also available to help customers. a listing of sales offices and locations is included in the back of this document. technical support is available through the website at: http://microchip.com/support downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 385 pic18(l)f1xk22 product identification system to order or obtain information, e. g., on pricing or delivery, refer to the factory or the listed sales office . part no. x /xx xxx pattern package temperature range device device: pic18f13k22, pic18lf13k22 PIC18F14K22, pic18lf14k22 tape and reel option: blank = standard packaging (tube or tray) t = tape and reel (1), (2) temperature range: e= - 4 0 ? c to +125 ? c (extended) i= - 4 0 ? c to +85 ? c (industrial) package: ml = qfn p=pdip so = soic ss = ssop pattern: qtp, sqtp, code or special requirements (blank otherwise) examples: a) PIC18F14K22-e/p 301 = extended temp., pdip package, qtp pattern #301. b) pic18lf14k22-e/so = extended temp., soic package. c) pic18lf14k22-e/ml = extended temp., qfn package. d) pic18f13k22t-i/ss = industrial temp., ssop package, tape and reel. note 1: tape and reel option is available for ml, mv, pt, so and ss packages with industrial temperature range only. 2: tape and reel identifier only appears in catalog part number description. this identifier is used for ordering purposes and is not printed on the device package. [x] (2) tape and reel option - downloaded from: http:///
ds40001365f-page 386 ? 2009-2016 microchip technology inc. information contained in this publication regarding device applications and the like is provided only for your convenience and may be superseded by updates. it is your responsibility to ensure that your application meets with your specifications. microchip makes no representations or warranties of any kind whether express or implied, written or oral, statutory or otherwise, related to the information, including but not limited to its condition, quality, performance, merchantability or fitness for purpose . microchip disclaims all liability arising from this information and its use. use of microchip devices in life support and/or safety applications is entirely at the buyers risk, and the buyer agrees to defend, indemnify and hold harmless microchip from any and all damages, claims, suits, or expenses resulting from such use. no licenses are conveyed, implicitly or otherwise, under any microchip intellectual property rights unless otherwise stated. trademarks the microchip name and logo, the microchip logo, dspic, flashflex, flexpwr, jukeblox, k ee l oq , k ee l oq logo, kleer, lancheck, medialb, most, most logo, mplab, optolyzer, pic, picstart, pic 32 logo, righttouch, spynic, sst, sst logo, superflash and uni/o are registered trademarks of microchip technology incorporated in the u.s.a. and other countries. the embedded control solutions company and mtouch are registered trademarks of microc hip technology incorporated in the u.s.a. analog-for-the-digital age, bodycom, chipkit, chipkit logo, codeguard, dspicdem, dspicdem.net, ecan, in-circuit serial programming, icsp, inter-chip connectivity, kleernet, kleernet logo, miwi, motorbench, mpasm, mpf, mplab certified logo, mplib, mplink, multitrak, netdetach, omniscient code generation, picdem, picdem.net, pickit, pictail, righttouch logo, real ice, sqi, serial quad i/o, total endurance, tsharc, usbcheck, varisense, viewspan, wiperlock, wireless dna, and zena are trademarks of microchip technology incorporated in the u.s.a. and other countries. sqtp is a service mark of microchip technology incorporated in the u.s.a. silicon storage technology is a registered trademark of microchip technology inc. in other countries. gestic is a registered trademark of microchip technology germany ii gmbh & co. kg, a subsidiary of microchip technology inc., in other countries. all other trademarks mentioned herein are property of their respective companies. ? 2009-2016, microchip technology incorporated, printed in the u.s.a., all rights reserved. isbn: 978-1-5224-0464-4 note the following details of the code protection feature on microchip devices: microchip products meet the specification contai ned in their particular microchip data sheet. microchip believes that its family of products is one of the most secure families of its kind on the market today, when used i n the intended manner and under normal conditions. there are dishonest and possibly illegal methods used to breach the code protection f eature. all of these methods, to our knowledge, require using the microchip products in a manner outsi de the operating specifications contained in microchips data sheets. most likely, the person doing so is engaged in theft of intellectual property. microchip is willing to work with the customer who is concerned about the integrity of their code. neither microchip nor any other semicond uctor manufacturer can guarantee the security of their code. code protection does not mean that we are guaranteeing the product as unbreakable. code protection is constantly evolving. we at microchip are committed to continuously improving the code protection features of our products. attempts to break microchips code protection feature may be a violation of the digital millennium copyright act. if such acts allow unauthorized access to your software or other copyrighted work, you may have a ri ght to sue for relief under that act. microchip received iso/ts-16949:2009 certification for its worldwide headquarters, design and wafer fabrication facilities in chandler and tempe, arizona; gresham, oregon and design centers in california and india. the company?s quality system processes and procedures are for its pic ? mcus and dspic ? dscs, k ee l oq ? code hopping devices, serial eeproms, microperipherals, nonvolatile memory and analog products. in addition, microchip?s quality system for the design and manufacture of development systems is iso 9001:2000 certified. quality management s ystem certified by dnv == iso/ts 16949 == downloaded from: http:///
? 2009-2016 microchip technology inc. ds40001365f-page 387 americas corporate office 2355 west chandler blvd. chandler, az 85224-6199 tel: 480-792-7200 fax: 480-792-7277 technical support: http://www.microchip.com/ support web address: www.microchip.com atlanta duluth, ga tel: 678-957-9614 fax: 678-957-1455 austin, tx tel: 512-257-3370 boston westborough, ma tel: 774-760-0087 fax: 774-760-0088 chicago itasca, il tel: 630-285-0071 fax: 630-285-0075 cleveland independence, oh tel: 216-447-0464 fax: 216-447-0643 dallas addison, tx tel: 972-818-7423 fax: 972-818-2924 detroit novi, mi tel: 248-848-4000 houston, tx tel: 281-894-5983 indianapolis noblesville, in tel: 317-773-8323 fax: 317-773-5453 los angeles mission viejo, ca tel: 949-462-9523 fax: 949-462-9608 new york, ny tel: 631-435-6000 san jose, ca tel: 408-735-9110 canada - toronto tel: 905-673-0699 fax: 905-673-6509 asia/pacific asia pacific office suites 3707-14, 37th floor tower 6, the gateway harbour city, kowloon hong kong tel: 852-2943-5100 fax: 852-2401-3431 australia - sydney tel: 61-2-9868-6733 fax: 61-2-9868-6755 china - beijing tel: 86-10-8569-7000 fax: 86-10-8528-2104 china - chengdu tel: 86-28-8665-5511 fax: 86-28-8665-7889 china - chongqing tel: 86-23-8980-9588 fax: 86-23-8980-9500 china - dongguan tel: 86-769-8702-9880 china - hangzhou tel: 86-571-8792-8115 fax: 86-571-8792-8116 china - hong kong sar tel: 852-2943-5100 fax: 852-2401-3431 china - nanjing tel: 86-25-8473-2460 fax: 86-25-8473-2470 china - qingdao tel: 86-532-8502-7355 fax: 86-532-8502-7205 china - shanghai tel: 86-21-5407-5533 fax: 86-21-5407-5066 china - shenyang tel: 86-24-2334-2829 fax: 86-24-2334-2393 china - shenzhen tel: 86-755-8864-2200 fax: 86-755-8203-1760 china - wuhan tel: 86-27-5980-5300 fax: 86-27-5980-5118 china - xian tel: 86-29-8833-7252 fax: 86-29-8833-7256 asia/pacific china - xiamen tel: 86-592-2388138 fax: 86-592-2388130 china - zhuhai tel: 86-756-3210040 fax: 86-756-3210049 india - bangalore tel: 91-80-3090-4444 fax: 91-80-3090-4123 india - new delhi tel: 91-11-4160-8631 fax: 91-11-4160-8632 india - pune tel: 91-20-3019-1500 japan - osaka tel: 81-6-6152-7160 fax: 81-6-6152-9310 japan - tokyo tel: 81-3-6880- 3770 fax: 81-3-6880-3771 korea - daegu tel: 82-53-744-4301 fax: 82-53-744-4302 korea - seoul tel: 82-2-554-7200 fax: 82-2-558-5932 or 82-2-558-5934 malaysia - kuala lumpur tel: 60-3-6201-9857 fax: 60-3-6201-9859 malaysia - penang tel: 60-4-227-8870 fax: 60-4-227-4068 philippines - manila tel: 63-2-634-9065 fax: 63-2-634-9069 singapore tel: 65-6334-8870 fax: 65-6334-8850 taiwan - hsin chu tel: 886-3-5778-366 fax: 886-3-5770-955 taiwan - kaohsiung tel: 886-7-213-7828 taiwan - taipei tel: 886-2-2508-8600 fax: 886-2-2508-0102 thailand - bangkok tel: 66-2-694-1351 fax: 66-2-694-1350 europe austria - wels tel: 43-7242-2244-39 fax: 43-7242-2244-393 denmark - copenhagen tel: 45-4450-2828 fax: 45-4485-2829 france - paris tel: 33-1-69-53-63-20 fax: 33-1-69-30-90-79 germany - dusseldorf tel: 49-2129-3766400 germany - karlsruhe tel: 49-721-625370 germany - munich tel: 49-89-627-144-0 fax: 49-89-627-144-44 italy - milan tel: 39-0331-742611 fax: 39-0331-466781 italy - venice tel: 39-049-7625286 netherlands - drunen tel: 31-416-690399 fax: 31-416-690340 poland - warsaw tel: 48-22-3325737 spain - madrid tel: 34-91-708-08-90 fax: 34-91-708-08-91 sweden - stockholm tel: 46-8-5090-4654 uk - wokingham tel: 44-118-921-5800 fax: 44-118-921-5820 worldwide sales and service 07/14/15 downloaded from: http:///


▲Up To Search▲   

 
Price & Availability of PIC18F14K22

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X